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 ...
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))
🌐
CodeRivers
coderivers.org › blog › how-to-create-listnode-in-python
Creating ListNodes in Python: A Comprehensive Guide - CodeRivers
February 22, 2026 - A ListNode in Python typically consists of two main parts: - Value: This is the data that the node holds. It can be any Python data type, such as an integer, string, or even another object. - Next Pointer: This is a reference to the next node in the linked list.
🌐
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.
Find elsewhere
🌐
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.
🌐
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...
🌐
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.
🌐
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 ·
🌐
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.
🌐
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 ...
🌐
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.