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:
resultandresult_tailare two variables that happen to point at the same value- Mutation / Changing of the underlying value (
result_tail.next = ListNode(1)) will affect the value shown byresult - However, assigning / pointing the variable
result_tailto another value will NOT affect the value ofresult result_tail = result_tail.nextis 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
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 - In this example, we will follow ... like so: 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 ...
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:
resultandresult_tailare two variables that happen to point at the same value- Mutation / Changing of the underlying value (
result_tail.next = ListNode(1)) will affect the value shown byresult - However, assigning / pointing the variable
result_tailto another value will NOT affect the value ofresult result_tail = result_tail.nextis 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.
- 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) + "})"
- 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:]))
- 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))
13:56
Linked Lists: The Secret to Efficient Python Code - YouTube
19:41
Linked List Node Creation Operation in Python - YouTube
08:45
Python: Linked Lists Explained | implementation, insert, delete ...
Python Data Structures #2: Linked List
12:11
[3.1] Create Node and Linked List Class | Data Structures in Python ...
Stack Abuse
stackabuse.com › python-linked-lists
Python Linked Lists
August 25, 2023 - For example, this object contains the following methods: append(): add an item to the right side of the list (end) append_left(): add an item to the left side of the list (head) ... The underlying data structure of deque is a Python list which is double-linked.
HotExamples
python.hotexamples.com › examples › - › ListNode › - › python-listnode-class-examples.html
Python ListNode Examples, ListNode Python Examples - HotExamples
def reorder(head): if not head: return None it = curr = head # construct a reverse list p_head = p = ListNode(it.val) length = 1 while it.next: it = it.next length += 1 p.next = ListNode(it.val) p = p.next tail = reverse(p_head) # construct the reordered list count = 0 while count < length: store = tail.next temp = curr.next curr.next = tail count += 1 if count == length: curr.next = None break tail.next = temp count += 1 if count == length: curr.next.next = None break curr = temp tail = store # show(head) return head
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
One example of a problem to practice involves reversing a linked list, a common operation in interviews and industry. To reverse a linked list, we'll need to sequentially rearrange the next link of each node to point toward its previous node. Here is what the code might look like.
Real Python
realpython.com › linked-lists-python
Linked Lists in Python: An Introduction – Real Python
June 24, 2026 - In terms of both speed and memory, implementing graphs using adjacency lists is very efficient in comparison with, for example, an adjacency matrix. That’s why linked lists are so useful for graph implementation. ... In most programming languages, there are clear differences in the way linked lists and arrays are stored in memory. In Python, however, lists are dynamic arrays.
CodeRivers
coderivers.org › blog › list-node-python
Exploring List Nodes in Python: Concepts, Usage, and Best Practices - CodeRivers
February 22, 2026 - Here is a simple Python class to represent a singly linked list node: class ListNode: def __init__(self, data): self.data = data self.next = None
CodeRivers
coderivers.org › blog › how-to-create-listnode-in-python
Creating ListNodes in Python: A Comprehensive Guide - CodeRivers
February 22, 2026 - For example, you could create a function to insert a node at a specific position or to delete a node with a given value. This promotes code reuse and makes the overall codebase more modular. Creating and working with ListNode in Python is an essential skill for anyone dealing with linked list-based ...
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.
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
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.
NBShare
nbshare.io › notebook › 212573943 › Why-do-we-use-Optional-ListNode-in-Python
Why do we use Optional ListNode in Python
def get_next_node(node: ListNode) -> Optional[ListNode]: if node is None: return None return node.next · In this example, the get_next_node() function takes a ListNode object as an argument and returns either another ListNode object or None, depending on whether the input node is None.
NeetCode
neetcode.io › courses › dsa-for-beginners › 5
NeetCode | Coding Interview Prep, Courses, Versus Mode
By chaining these ListNode objects together we can build a linked list. We start with a ListNode class: Python · Java · C++ JavaScript · C# Go · Kotlin · Swift · class ListNode: def __init__(self, val): self.val = val self.next = None ·