Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Logic of ListNode in Leetcode

Here is the definition of ListNote class in LeetCode:

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

For the code:

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

The values in comments are from my guessing. I cannot understand the step

result_tail = result_tail.next

result_tail = result is pass by reference, so when result_tail becomes 1 -> None, result should also become 1 -> None. Why does result still keep 0 -> 1 -> None? And when result_tail becomes 1 -> 2 -> None, why does result extend its tail to 0 -> 1 -> 2 -> None?

result_tail = result_tail.next

is something like

result_tail = result.next.next    

Can anyone tell me the logic here?

like image 814
user6703592 Avatar asked Jun 09 '19 15:06

user6703592


People also ask

What does ListNode () do in Python?

A node is implemented as a class named ListNode . The class contains the definition to create an object instance, in this case, with two variables - data to keep the node value, and next to store the reference to the next node in the list.

How do you access linked list elements in Python?

A linked list is created by using the node class we studied in the last chapter. We create a Node object and create another class to use this ode object. We pass the appropriate values through the node object to point the to the next data elements. The below program creates the linked list with three data elements.

What is a ListNode object?

This is the a node for a singly-linked list, which is capable of holding an type of Object. A ListNode consists of two data members: The data we are keeping track of at this node (Object)

How do you traverse a linked list in Python?

In order to traverse a linked list, you just need to know the memory location or reference of the first node, the rest of nodes can be sequentially traversed using the reference to the next element in each node. The reference to the first node is also known as the start node.


1 Answers

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
like image 189
Joseph Avatar answered Nov 15 '22 16:11

Joseph