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
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))
๐ŸŒ
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 at the end of the list.
๐ŸŒ
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
๐ŸŒ
Statistics Globe
statisticsglobe.com โ€บ home โ€บ python programming language for statistics & data science โ€บ what is a list node in python? (2 examples)
What is a List Node in Python? (2 Examples) | Linked List Structure
May 15, 2023 - class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next # instantiate the nodes node1 = ListNode("The") node2 = ListNode("boy") node3 = ListNode("is") node4 = ListNode("tall") # link the nodes node1.next = node2 node2.next = node3 node3.next = node4 # traverse the linked list and print each node's value current_node = node1 while current_node is not None: print(current_node.val) current_node = current_node.next # The # boy # is # tall
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [deleted by user]
[deleted by user] : r/learnpython
December 5, 2023 - ListNode(total), per the constructor, that's how the __init__ function is always called.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ listnode-python
Understanding and Using ListNode in Python - CodeRivers
July 21, 2026 - Here is a simple Python class ... next: Optional[ListNode] = None) -> None: self.val = val self.next = next ยท In this code, the __init__ method is used to initialize a ListNode....
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-linked-lists
Python Linked Lists: Tutorial With Examples | DataCamp
June 2, 2026 - Every time you call the above method, a new node is created with your specified data. The next pointer of this new node is set to the current head of the list, which will place this node in front of the existing nodes.
Find elsewhere
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ list-node-python
Exploring List Nodes in Python: Concepts, Usage, and Best Practices - CodeRivers
February 22, 2026 - def delete_node(head, key): if head is None: return head if head.data == key: return head.next current = head while current.next is not None and current.next.data != key: current = current.next if current.next is not None: current.next = current.next.next return head # Example usage head = ListNode(10) head.next = ListNode(20) head = delete_node(head, 20) A singly linked list is a sequence of list nodes where each node has a reference to the next node. Here is a more complete implementation of a singly linked list in Python:
๐ŸŒ
W3Schools
w3schools.com โ€บ Python โ€บ python_dsa_linkedlists.asp
Linked Lists with Python
Python Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join Lists List Methods List Exercises Code Challenge Python Tuples
๐ŸŒ
CSCI 0112
cs0112.github.io โ€บ Lectures โ€บ lecture19.html
Linked Lists (Part 1) | CSCI 0112 - Fall 2024
October 21, 2024 - # internal helper method, not called from outside the class # we'll use the double-underscore convention to label this as "private" def __append_to(self, node: ListNode, data): if not node.next: node.next = ListNode(data) else: self.__append_to(node.next, data) # this is the method that a caller would invoke def append(self, data): if not self.first: self.fst = ListNode(data) else: self.__append_to(self.first, data) And now weโ€™ve written append. Or have we? We couldnโ€™t write tests easily before, but now we can. And next time weโ€™ll have other methods that we can use to make our tests even better. (I suggest looking at how the append method works in the VSCode debugger; it will help you see the sequence of steps that Python is taking to add elements to a progressively longer list.)
๐ŸŒ
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
๐ŸŒ
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-linked-list
Python Linked List - GeeksforGeeks
December 11, 2025 - Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
๐ŸŒ
NBShare
nbshare.io โ€บ notebook โ€บ 212573943 โ€บ Why-do-we-use-Optional-ListNode-in-Python
Why do we use Optional ListNode in Python
Optional[ListNode] is a type hint in Python that indicates that a function or variable can have a value of either ListNode or None.
๐ŸŒ
Real Python
realpython.com โ€บ linked-lists-python
Linked Lists in Python: An Introduction โ€“ Real Python
June 24, 2026 - The method above goes through the list and yields every single node. The most important thing to remember about this __iter__ is that you need to always validate that the current node is not None.
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how does listnode work in leetcode?
r/learnprogramming on Reddit: How does ListNode work in LeetCode?
July 3, 2024 -

I've decided to tackle my first medium problem on Leetcode, Add Two Numbers (Java), but I've hit a wall. I started by reading the task and looking at the description, but after doing some research I just can't grasp how the ListNode class works. Here is its code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */

I understand that it is supposed to work similar to the java.util.LinkedList class, but I don't get how. It holds two methods besides the default constructor and they only seem to set the values of int val and ListNode next. I am lost at how this chunk of code creates a singly-linked list, as I learned how to use one, but not how it works under the hood. I thought maybe it had something to do with the keyword "this", but after researching even more I learned that it is used to refer to the class property and not the parameter.

Also, while I was working on this I also wondered, is there a way to check the class files for the standard library or any other library? I want to check the code for the LinkedList and other classes in the future for clarity.

๐ŸŒ
pytz
pythonhosted.org โ€บ llist
llist โ€” Linked list datatypes for Python โ€” llist 0.4 documentation
Note that value stored in the node can also be obtained through the __call__() method (using standard node() syntax).
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ how-to-create-listnode-in-python
Creating ListNodes in Python: A Comprehensive Guide - CodeRivers
February 22, 2026 - To create a ListNode in Python, ... def __init__(self, val=0, next=None): self.val = val self.next = next ยท In this code: - The __init__ method is the constructor of the ListNode class....