Drawing diagrams helps. Here's your linked list:

Copy[   ]
  |
  v
[   ]
  |
  V
[   ]
  |
  V
 None

Each arrow leading from a box represents the next attribute of that node.

Here are the three variables a, b, and c:

Copy         [   ] <-- a
           |
           v
         [   ] <-- b
           |
           V
         [   ] <-- c
           |
           V
          None

Each of these variables also points to a particular node.

If you say b.next = None, the next attribute of the node referenced by b is modified, like this:

Copy         [   ] <-- a
           |
           v
None <-- [   ] <-- b


         [   ] <-- c
           |
           V
          None

This modifies the structure of the list. If you just set b itself to a different value, though, this is what happens:

Copy         [   ] <-- a
           |
           v
None <-- [   ]     b --> None


         [   ] <-- c
           |
           V
          None

You changed b, but the node that b used to point to stays right where it was. Note that this is similar to how the c node continued to exist even after you set b.next = None.

Answer from Samwise on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ 70+ python leetcode problems solved in 5+hours (every data structure)
r/Python on Reddit: 70+ Python Leetcode Problems solved in 5+hours (every data structure)
October 3, 2024 -

https://m.youtube.com/watch?v=lvO88XxNAzs

I love Python, itโ€™s my first language and the language that got me into FAANG (interviews and projects).

Itโ€™s not my day to day language (now TypeScript) but I definitely think itโ€™s the best for interviews and getting started which is why I used it in this video.

Included a ton of Python tips, as well as programming and software engineering knowledge. Give a watch if you want to improve on these and problem solving skills too ๐Ÿซก

๐ŸŒ
LeetCode
leetcode.com โ€บ problemset
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
๐ŸŒ
GitHub
github.com โ€บ cnkyrpsgl โ€บ leetcode
GitHub - cnkyrpsgl/leetcode: All Python solutions for Leetcode
This repository includes my solutions to all Leetcode algorithm questions. This problems mostly consist of real interview questions that are asked on big companies like Facebook, Amazon, Netflix, Google etc. If you find my solutions hard to comprehend, give yourself a time to solve easier questions or check discussion section to problem on LeetCode.
Starred by 486 users
Forked by 211 users
Languages ย  Python
๐ŸŒ
LeetCode
leetcode.com โ€บ discuss โ€บ study-guide โ€บ 2122306 โ€บ python-cheat-sheet-for-leetcode
Python Cheat Sheet for Leetcode - Discuss - LeetCode
2nd argument's default is None. dict.update({KEY:VALUE}) # inserts pair in dictionary if not present, if present, corresponding value is overriden (not key) # defaultdict ensures that if any element is accessed that is not present in the dictionary # it will be created and error will not be thrown (which happens in normal dictionary) # Also, the new element created will be of argument type, for example in the below line # an element of type 'list' will be made for a Key that does not exist myDictionary = defaultdict(list) Python Counter is a container that will hold the count of each of the elements present in the container.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ solve-leetcode-problems-using-python-list-comprehension
How to Solve Leetcode Problems With Python One-Liners
April 2, 2021 - You can see how the solution using list comprehension is simplified from 6 lines to 1 line. This is the power of list comprehension. Now let us solve the below Leetcode problems in 1 line using list comprehension.
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ minimum-domino-rotations-for-equal-row โ€บ solutions โ€บ 252662 โ€บ python-3-lists
Minimum Domino Rotations For Equal Row - LeetCode
Minimum Domino Rotations For Equal Row - In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
Find elsewhere
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ insert-delete-getrandom-o1-duplicates-allowed โ€บ solutions โ€บ 85611 โ€บ concise-python-solution-with-list-dict-and-set
Insert Delete GetRandom O(1) - Duplicates allowed - LeetCode
Can you solve this real interview question? Insert Delete GetRandom O(1) - Duplicates allowed - RandomizedCollection is a data structure that contains a collection of numbers, possibly duplicates (i.e., a multiset).
๐ŸŒ
GitHub
github.com โ€บ Garvit244 โ€บ Leetcode
GitHub - Garvit244/Leetcode: ๐ŸŽ“Leetcode solutions in Python ๐Ÿ“š
Python solution of problems from LeetCode.
Starred by 1.5K users
Forked by 642 users
Languages ย  Python
๐ŸŒ
GitHub
github.com โ€บ qiyuangong โ€บ leetcode
GitHub - qiyuangong/leetcode: Python & JAVA Solutions for Leetcode
Build a char count list with 26-256 length. Note that this list can be update when going through the string.
Starred by 5.3K users
Forked by 1.5K users
Languages ย  Python 75.2% | Java 22.6% | C++ 2.2%
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ design-hashmap โ€บ discuss โ€บ 1098212 โ€บ python-list-methods
[Python] List Methods - Design HashMap
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
๐ŸŒ
Medium
medium.com โ€บ @sunshine990316 โ€บ leetcode-python-linked-list-summary-easy-1-a8c36ceb169e
Leetcode (Python) โ€” Linked List summary Easy 1 | by Sunshine | Medium
July 7, 2023 - # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def mergeTwoLists(self, list1, list2): """ :type list1: Optional[ListNode] :type list2: Optional[ListNode] :rtype: Optional[ListNode] """ dummy = ListNode() tail = dummy while list1 and list2: if list1.val < list2.val: tail.next = list1 list1 = list1.next else: tail.next = list2 list2 = list2.next tail = tail.next if list1: tail.next = list1 elif list2: tail.next = list2 return dummy.next
๐ŸŒ
Udemy
udemy.com โ€บ it & software
DSA In Python + Top 130 Leetcode Problems for MAANG
November 5, 2025 - Learn python list slicing, with start point, end point, and jump, producing non-inclusive sublists and defaults, while recognizing syntactic sugar that speeds coding for competitive programming. ... Compute max consecutive ones in a binary array with a running count and best value, resetting on zeros; this linear O(n) approach relates to the maximum sum subarray. Leetcode #121 - Best Time To Buy And Sell Stock - Python9:01
Rating: 4.6 โ€‹ - โ€‹ 361 votes
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74359459 โ€บ linked-list-problem-leetcode-understanding-inputs
python - Linked list problem (Leetcode) - understanding inputs - Stack Overflow
If I return seen, it gives the correct values but in the form of a set whereas 'the expected return type is ListNode' 2022-11-08T11:19:50.32Z+00:00 ... Now head just returns as output the same inputs unchanged. 2022-11-08T11:26:16.98Z+00:00 ... Can you give me the link to the problem so that I can try because it is becoming difficult to guess the correct output 2022-11-08T11:28:44.45Z+00:00 ... Yes, of course: leetcode.com/problems/remove-duplicates-from-sorted-list 2022-11-08T11:32:59.437Z+00:00
๐ŸŒ
LeetCode
leetcode.com โ€บ problemset
LeetCode Python Problems
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
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 Overflow
stackoverflow.com โ€บ questions โ€บ 75089767 โ€บ leetcode-problem-asks-for-the-output-to-be-in-listlistint-but-returns-a-set
python - Leetcode problem asks for the output to be in List[List[int]] but returns a set without converting into a list - Stack Overflow
indeed, but the set, in this case, gets seen as a List[List[int]]. This should also work return [list(i) for i in list(res)] this one seems to be a little faster but takes more memory it seems. ... Typings in python aren't enforced in any way. You can write ... And everything will work smoothly (you will be warned by some linters like mypy, but nothing that stops this from executing) So there is no problem on your side with the code. But the second question is, what leetcode does to check your answer?
๐ŸŒ
Udemy
udemy.com โ€บ it & software
50 Days of LeetCode in Python: Algorithms Coding Interviews
July 6, 2025 - Implement the code to check if a list of integers forms a valid mountain array, returning a boolean, by ascending then descending to reach the end. ... Introduces the boats to save people problem from LeetCode, where at most two people per boat must sum to โ‰ค limit, and shows computing the minimum number of boats.
Rating: 4.7 โ€‹ - โ€‹ 1.69K votes