0

I am trying to reverse a linked list in Java.
Here is my code.

class Node{ 
    int value; Node next; 
    Node(int value){ this.value = value; } 
} 

class LinkedList{

 Node head;

 void push(int value){
        Node new_node = new Node(value);
        new_node.next = head;
        head = new_node;
        System.out.println("new head is"+head.value);
    }

void reverseList(Node node){
        Node iter = node;
        Node prev = null;
        while(iter!=null){
            Node currentNext = iter.next;
            iter.next = prev;
            prev = iter;
            iter = currentNext;
        }
        node = prev;
    }
// To print the list referred by this node.
void printList(Node node){
        Node iter = node;
        while(iter!=null){
            System.out.print(" "+iter.value);
            iter = iter.next;
        }
        System.out.println("");
    }
}

In main(),

LinkedList list = new LinkedList();
list.push(3);
list.push(2);
list.push(1); 
list.printList();// linked list is 1->2->3
list.reverseList(list.head);
list.printList(list.head); // Output is 1

Why is the list not printing in the correct orde (3->2->1) after reversing the list?

c0der
  • 18,467
  • 6
  • 33
  • 65
Niharika Epuri
  • 55
  • 1
  • 1
  • 7

2 Answers2

0

You reversed the list without reseting the head.

Your print reference is Node 1, which is the tail now

Note that node = prev doesn't actually do anything but reassign the parameter, which is usually never a good idea

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • In main(), I called the method by, `list.reverseList(list.head);` And at the end of the `reverseList(Node node)` definition, I reseted the node to previous `(node=prev)`. So, here, doesn't It mean list.head = prev? Why will this not work? – Niharika Epuri Aug 26 '17 at 07:05
  • Not sure I understand, but I see nowhere but the constructor that you've set `list.head` – OneCricketeer Aug 26 '17 at 07:07
  • In main(), I called the method by, list.reverseList(list.head); And at the end of the reverseList(Node node) definition, I reseted the node to previous (node=prev). So, here, doesn't It mean list.head = prev? Why will this not work? – Niharika Epuri Aug 26 '17 at 07:10
  • As I said `node = prev` is not the same as explicitly putting `this.head = prev` – OneCricketeer Aug 26 '17 at 07:11
  • It won't work because all you did was reassign the reference of the parameter, not the instance variable of the class.... It's like expecting `Node iter = node;` and modifying iter to somehow affect `node` – OneCricketeer Aug 26 '17 at 07:13
  • @NiharikaEpuri Because Java is not pass-by-reference. The fallacy is right there in your title. Read the duplicate. – user207421 Aug 26 '17 at 07:16
  • In java if reference is changed then won't the instance variable also change? – Niharika Epuri Aug 26 '17 at 07:17
  • @NiharikaEpuri No, because Java is not pass-by-reference, as I have just told you, contrary to the assertion in your title, and as is clearly answered several times in the duplicate. – user207421 Aug 26 '17 at 07:18
  • Ya got it. Thank alot :) – Niharika Epuri Aug 26 '17 at 09:07
  • I wonder why this was down voted ? It answers "Why is the list not printing in the correct orde (3->2->1) after reversing the list?" – c0der Aug 26 '17 at 09:22
0

Change node = prev; to head = prev; or better reverse the linked list by :

void reverseList(){

    Node iter = head;
    Node prev = null;

    while(iter!=null){

        Node currentNext = iter.next;
        iter.next = prev;
        prev = iter;
        iter = currentNext;
    }

    head = prev;
}
c0der
  • 18,467
  • 6
  • 33
  • 65