NeverGiveUp
NeverGiveUp

Reputation: 33

Simple linked list ques

So in the first code, I am making a.next=b. Which means both a.next and b acts as a reference to Node6. SO when I am making b=None.. Why is a.next still pointing to Node6,

In the second code, when I am aking c=a, both c and a are references to Node 5. Now I am making c.next=None, But this change is being reflected to a also. even a.next becomes None.

BOTH THE CODE I MADE SAME STEPS, Why is the change only reflected in code 2?

class Node:

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

a=Node(5)
b=Node(6)
a.next=b
b=None
while a is not None:
    print(a.val)
    a=a.next

CODE 2

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

a=Node(5)
b=Node(6)
a.next=b
c=a
c.next=None
while a is not None:
    print(a.val)
    a=a.next

Upvotes: 1

Views: 70

Answers (3)

Ankita Mehta
Ankita Mehta

Reputation: 600

Let me explain using a diagrammatic representation: enter image description here

In case 1: List a has two nodes 5, and 6 in linked list. But In case 2: List a and c have one node 5 in linked list. And another linked list is pointed by b which has only 6.

Hope this clarifies your doubt.

NOTE: - Since a,b, c are pointers storing addresses. When you execute c.next=None, the value of next at address pointed by c (also pointed by a) becomes None.

Upvotes: 3

Booboo
Booboo

Reputation: 44108

You have a class Node. When you create an object of that class and assign it to a variable such as a or b, you are dynamically allocating memory large enough to hold the contents of the object and then assigning the address of that memory to the variable. So CODE 1 really looks more like:

addr1 = storage address for Node(5) // Statement 1
a = addr1 // Statement 2
addr2 = storage address for Node(6) // Statement 3
b = addr2 // Statement 4
a.next = b // Statement 5
b = None // Statement 6

Statement 5 is updating the next attribute of the Node that was allocated in Statement 1 with the value of b, which is addr2. In Statement 6, we are assigning a new value to b, but that will not change what was stored into the actual object referenced by a. Consider the following example with integers:

x = 5
y = 7
x = y
y = 0

Setting y = 0 doesn't affect the previous assignment of x = y, i.e. x will still have y's previous value of 7. The only difference i your example is that we are not dealing with integers but with addresses.

Now that we appreciate that a and b are really references to memory locations (i.e. addresses) that hold the actual objects, when you say c=a, c and a both refer now to the same object and can be used interchangeably. Thus, c.next and a.next refer to the same attribute.

Upvotes: 1

Akash Pagar
Akash Pagar

Reputation: 637

In the first case, you are storing b in a.next, so while iterating through linked list wit head node as a you traverse all the nodes.

But in the second case c and a having the same refernce, and c.next is None so whenever you traverse the linked list with head as c you get to only one node. As while a is not None condition not allowing to move for the next node.

Following code works as expected.

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


a = Node(5)
b = Node(6)
c = a
c.next = b #this line changed
while a is not None:
    print(a.val)
    a = a.next

Upvotes: 0

Related Questions