Why head getting changed even though i copy it in another Node called curr.? It's happening only after reversing my linked List. | Sololearn: Learn to code for FREE!

+1

Why head getting changed even though i copy it in another Node called curr.? It's happening only after reversing my linked List.

https://code.sololearn.com/c8xj8PTuAL2f/?ref=app

8/7/2022 12:40:37 PM

Surya T

17 Answers

New Answer

+2

Comment line 36: current.next = prev; will set head.next to prev which is null.

+2

That is to reverse linked list right if i comment it mu linked list won't get reversed.

+2

If we need to reverse, head node should become tail node(tail node address is null). curr.next does this, while looping i make move prev and head one node right.

+2

So Even if i copy head to curr node and use it, changes made in curr node will alse change head. Am I right? Mr.Jayakrishna

+2

Yes. As per your method, how you go back to previous node. You don't have previous node for your Node object..? I think it's not work. What is your reverse() method do?

+2

Node curr = head; Node prev = null; While(curr != null){ //Storing node next to head in nextNode. so that we won't loose data. Node nextNode = curr.next; //Now the curr.next made null(so that it becomes tail) curr.next = prev; //Now the current Node is made as prev. (this is my previous node now) prev = curr; //Now the previously next Node is made as current head Node. (this is my head node now) curr = nextNode; }

+2

Prev is my reversed linked list.

+2

" curr.next made null ( so that it becomes tail) " How it becomes tail? Tail is the last node your added which last 1. 1-> 2->1->1 If current is 1 then next is 2 Every time you copy current into prev because at last curr.next becomes null at loop end. By this method, you are finding last entered value, not previously entered value... And Yes. curr = head; copies references..

+2

1->2->3 prev. curr. null. 1:ptr2 -> 2:ptr3 -> 3:null nextNode = curr.next.(i.e., Node 2:ptr3) curr.next = prev; Prev = curr prev null:1. 2:ptr3-> 3: null Curr = nextNode prev. curr null:1 2:ptr3 -> 3:null ----------------------------------------------------------------------- prev. curr. null:1 2:ptr3 -> 3:null nextNode = curr.next.(i.e., Node 3:null) curr.next = prev; Prev = curr prev null:1<-2:ptr1 3: null Curr = nextNode prev. curr null:1<-2:ptr3 3:null -------------------------------------------------- prev. curr. null:1<-2:ptr3 3:null nextNode = curr.next.(i.e., null) curr.next = prev; Prev = curr prev null:1<-2:ptr1 <-3: ptr2 Curr = nextNode prev. curr null:1<-2:ptr1 <-3:ptr2. null ----------------------------------------------- prev = 3:ptr2 -> 2:ptr1->1:null Curr = head = null.

+1

I understood how head becomes null

+1

Thank you so much for your Answers

+1

Your pointings are not clear to me.. 1->2->3 Node prev = null; Node nextNode = curr.next; curr.next = prev; prev = curr; curr = nextNode; Prev : curr.val Null : 1 First line in loop, Node nextNode = curr.next; stores your next node (2) into newNode.. curr.next = prev; this assigns curr to null ( so head.next also assigned to null). Next Prev = curr assign null pointer but prev.val is still 1 Curr = nextNode ; this stores back next node ->2 ______________ Node nextNode = curr.next; stores your next node (3) into newNode.. curr.next = prev; this assigns Node(2) pointer which is due to prev<=curr<=newNode previous iteration. Next Prev = curr assign Node(2) to prev Curr = nextNode ; this stores back next node ->3 ________ Node nextNode = curr.next; stores null end node into newNode.. curr.next = prev; this assigns Node(3) pointer which is due to prev<=curr<=newNode previous iteration. Next Prev = curr assign Node(3) to prev Curr = nextNode \\ null

+1

If you comment curr.next = prev then it won't set null and head as well.. But curr getting back next node because of curr = nextNode; But here head is no longer pointing to curr. So won't affect on head... Hope it helps..

+1

I understood the mistake there. Thank you for your Answers.

0

This is how my code works

0

Can you clarify me your above point (because i am still confused) with my explanation or correct me if i am wrong

0

Curr.next stores the previous nodes address. If I comment curr.next = prev; I won't get reversed Linked list. And as per my code my head should become null. As i create new linked list (which is reversed) prev. So I have to do inplace reversing to get Head itself as my reversed linked list.