The short answer to this is that, Python is a pass-by-object-reference language, not pass-by-reference as implied in the question. It means that:

  1. result and result_tail are two variables that happen to point at the same value
  2. Mutation / Changing of the underlying value (result_tail.next = ListNode(1)) will affect the value shown by result
  3. However, assigning / pointing the variable result_tail to another value will NOT affect the value of result
  4. result_tail = result_tail.next is assigning the next node of the node that is currently assigned by the variable

The following is an visualization of the values that are assigned to the variables (r = result, rt = result_tail):

result = ListNode(0)
#r
#0 -> None

result_tail = result
#r
#0 -> None
#rt

result_tail.next = ListNode(1)
#r
#0 -> 1 -> None
#rt

result_tail = result_tail.next
#r
#0 -> 1 -> None
#     rt

result_tail.next = ListNode(2)
#r
#0 -> 1 -> 2 -> None
#     rt

result_tail = result_tail.next
#r
#0 -> 1 -> 2 -> None
#          rt

References for additional reading:

  • An article explaining the Python pass-by-object reference style in detail https://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/
  • An answer explaining Python's pass-by-object reference style https://stackoverflow.com/a/33066581/12295149
  • Question asking on Python's object reference style Understanding Python's call-by-object style of passing function arguments
Answer from Joseph on Stack Overflow
🌐
CodeSignal
codesignal.com › learn › courses › getting-deep-into-complex-algorithms-for-interviews-with-python › lessons › linked-list-operations-in-python
Linked List Operations in Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverse_linked_list(head): prev = None current = head while current: next_node = current.next current.next = prev prev = current current = next_node return prev # Test head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))) reversed_head = reverse_linked_list(head) while reversed_head: print(reversed_head.val, end=" ") reversed_head = reversed_head.next # Output: 5 4 3 2 1
🌐
W3Schools
w3schools.com › python › python_dsa_linkedlists.asp
Linked Lists with Python
A Linked List is, as the word implies, a list where the nodes are linked together. Each node contains data and a pointer.
🌐
Stack Abuse
stackabuse.com › python-linked-lists
Python Linked Lists
August 25, 2023 - Adding items to the list is done via add_list_item(). This method requires a node as an additional parameter. To make sure it is a proper node (an instance of class ListNode) the parameter is first verified using the built-in Python function isinstance(). If successful, the node will be added ...
🌐
CodeRivers
coderivers.org › blog › listnode-python
Understanding and Using ListNode in Python - CodeRivers
July 21, 2026 - If you do not frequently add or remove elements at the beginning of your data structure, a Python list is usually the better choice. All linked list operations use O(1) auxiliary space (excluding the nodes themselves). Using type hints in your linked list code improves readability and helps catch bugs before runtime. As shown in the class definition earlier, annotate next as Optional[ListNode] and use from __future__ import annotations for forward references.
Top answer
1 of 4
27

The short answer to this is that, Python is a pass-by-object-reference language, not pass-by-reference as implied in the question. It means that:

  1. result and result_tail are two variables that happen to point at the same value
  2. Mutation / Changing of the underlying value (result_tail.next = ListNode(1)) will affect the value shown by result
  3. However, assigning / pointing the variable result_tail to another value will NOT affect the value of result
  4. result_tail = result_tail.next is assigning the next node of the node that is currently assigned by the variable

The following is an visualization of the values that are assigned to the variables (r = result, rt = result_tail):

result = ListNode(0)
#r
#0 -> None

result_tail = result
#r
#0 -> None
#rt

result_tail.next = ListNode(1)
#r
#0 -> 1 -> None
#rt

result_tail = result_tail.next
#r
#0 -> 1 -> None
#     rt

result_tail.next = ListNode(2)
#r
#0 -> 1 -> 2 -> None
#     rt

result_tail = result_tail.next
#r
#0 -> 1 -> 2 -> None
#          rt

References for additional reading:

  • An article explaining the Python pass-by-object reference style in detail https://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/
  • An answer explaining Python's pass-by-object reference style https://stackoverflow.com/a/33066581/12295149
  • Question asking on Python's object reference style Understanding Python's call-by-object style of passing function arguments
2 of 4
16

For those reading this in the future: I wanted to debug linked list problems on a local environment so here is what I did.

  1. Modified the Leetcode code for ListNode by including the dunder "repr" method. This is for when you want to print a ListNode to see what its value and next node(s).
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

    def __repr__(self):
        return "ListNode(val=" + str(self.val) + ", next={" + str(self.next) + "})"
  1. Next, I made a recursive function that makes a nested ListNode when you pass in a list. This is so you can test your methods by passing in lists (instead of having to manually make a confusing looking ListNode yourself.
def list_to_LL(arr):
    if len(arr) < 1:
        return None

    if len(arr) == 1:
        return ListNode(arr[0])
    return ListNode(arr[0], next=list_to_LL(arr[1:]))
  1. Here is an example that tests my answer for the "reverseList" problem:
def reverseList(head: ListNode) -> ListNode:
    prev = None
    while head:
        next_node = head.next
        head.next = prev
        prev = head
        head = next_node

    return prev


# test cases
t1 = list_to_LL([1, 2, 3, 4, 5])  #ListNode(val=1, next={ListNode(val=2, next={ListNode(val=3, next={ListNode(val=4, next={ListNode(val=5, next={None})})})})})
t2 = list_to_LL([1, 2])  #ListNode(val=1, next={ListNode(val=2, next={None})})
t3 = list_to_LL([])

# answers
print(reverseList(t1))
print(reverseList(t2))
print(reverseList(t3))
🌐
CSCI 0112
cs0112.github.io › Lectures › lecture19.html
Linked Lists (Part 1) | CSCI 0112 - Fall 2024
October 21, 2024 - Let’s make some classes that let us turn this into something we can write in Python. The picture above is a hint: we should probably have a kind of object to represent those links. class ListNode: def __init__(self, data): self.data = data self.next = None
🌐
Real Python
realpython.com › linked-lists-python
Linked Lists in Python: An Introduction – Real Python
June 24, 2026 - Learn Python linked lists, deques, and circular & doubly linked structures with practical examples and efficient operations. Get the Source Code: Click here to get the source code you’ll use to learn about linked lists in this tutorial.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-linked-list
Python Linked List - GeeksforGeeks
December 11, 2025 - Tutorials · Interview Prep · Python Tutorial · Data Types · Interview Questions · Examples · Quizzes · DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 11 Dec, 2025 · A linked list is a type of linear data structure individual items are not necessarily at contiguous locations.
🌐
YouTube
youtube.com › watch
Python: Linked Lists (fast) - YouTube
Linked Lists explained (fast) with animated example, and how to write a Linked List program in Python 3, with add, remove, find and size functions example co...
Published: May 21, 2015
🌐
freeCodeCamp
freecodecamp.org › news › introduction-to-linked-lists-in-python
Linked Lists in Python – Explained with Examples
September 22, 2022 - By Fakorede Damilola Different programming languages offer different ways to store and access data. Some of the data structures you can use are collections such as arrays, lists, maps, sets, and so on. These all do an awesome job storing and accessi...
🌐
Python Forum
python-forum.io › thread-31071.html
How to create a linked list and call it?
I want to create a linked list and insert dummy data to verify it. Please see my code. class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next a = ListNode(2) a.next = Li
🌐
Built In
builtin.com › data-science › python-linked-list
An Introduction to Python Linked List and How to Create One
Summary: A Python linked list is a linear data structure of nodes that store data and a reference to the next node. Unlike arrays, it allows efficient insertions and deletions without shifting elements.
🌐
TutorialsPoint
tutorialspoint.com › python_data_structure › python_linked_lists.htm
Python - Linked Lists
A linked list is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in form of a pointer. Python does not have linked lists in its standard library.
🌐
GitHub
gist.github.com › Jay87682 › 3935ac67616875e043d590c0b8b19f6d
python listnode · GitHub
python listnode · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
Educative
educative.io › answers › how-to-create-a-linked-list-in-python
How to create a Linked List in Python
A linked list is a data structure made of a chain of node objects. Each node contains a value and a pointer to the next node in the chain · Linked lists are preferred over arrays due to their dynamic size and ease of insertion and deletion ...
🌐
Reddit
reddit.com › r/learnpython › [deleted by user]
[deleted by user] : r/learnpython
December 5, 2023 - # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next totalstr = str(total) for i in range(len(totalstr): l1 = ListNode(int(totalstr[i], l2) But this will keep overwriting l1. How do I programmatically generate the next nodes? ... No one will use a linked list in python, especially when python is itself written in another language under the hood.
🌐
LeetCode
leetcode.com › problems › add-two-numbers › discuss › 174970 › Help-with-ListNode-(Python)
Help with ListNode (Python) - Add Two Numbers
Can you solve this real interview question? Add Two Numbers - You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit.
Top answer
1 of 2
3

To get the output you have listed at the end of your question, you would need to create a linked list. For instance, if you define the following class:

class ListNode:
    def __init__(self, val, nxt=None):
        self.val = val
        self.next = nxt

    def __repr__(self):
        return f"ListNode{{val: {self.val}, next: {self.next}}}"

And if you then define list1 as follows:

list1 = ListNode(1, ListNode(2, ListNode(4, None)))

Then the "commands" will give the output that you listed.

List to Linked List

If you want to create the above linked list from the list [1,2,4], then use this function:

def createLinkedList(values):
    head = None
    for val in reversed(values):
        head = ListNode(val, head)
    return head

Now you can convert a plain list to a linked list as follows:

list1 = createLinkedList([1,2,4])

Linked List to list

If you want to do the opposite, and create a standard list from a linked list, then define this function:

def linkedListIterator(head):
    while head:
        yield head.val
        head = head.next

Now, if you have a linked list, you can pass it to the above function. For instance:

list1 = createLinkedList([1,2,4])
lst = list(linkedListIterator(list1))

lst will be [1,2,4]

2 of 2
0

As mentioned in the comments, it appears as though you are confusing a list with a singly linked list.

With the list provided you would just iterate with a for loop like this:

list1=[1,2,4]


for L in list1:
    print(L)

Which gives the output:

1
2
4

For linked lists, please refer to this: https://www.tutorialspoint.com/python_data_structure/python_linked_lists.htm