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?
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.
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.
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)
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.
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:
result
and result_tail
are two variables that happen to point at the same valueresult_tail.next = ListNode(1)
) will affect the value shown by result
result_tail
to another value will NOT affect the value of result
result_tail = result_tail.next
is assigning the next node of the node that is currently assigned by the variableThe 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With