Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Singly Linked List Java [duplicate]

Can someone tell me why my code dosent work? I want to reverse a single linked list in java: This is the method (that doesnt work correctly)

public void reverseList(){     Node before = null;     Node tmp = head;     Node next = tmp.next;     while(tmp != null){       if(next == null)          return;       tmp.next = before;       before = tmp;       tmp = next;       next = next.next;     } } 

And this is the Node class:

public class Node{    public int data;    public Node next;    public Node(int data, Node next){       this.data = data;       this.next = next;    } } 

On input 4->3->2->1 I got output 4. I debugged it and it sets pointers correctly but still I dont get why it outputs only 4.

like image 316
sammy333 Avatar asked Mar 24 '14 09:03

sammy333


People also ask

How do you reverse a singly linked list?

The recursive approach to reverse a linked list is simple, just we have to divide the linked lists in two parts and i.e first node and the rest of the linked list, and then call the recursion for the other part by maintaining the connection.

How do you reverse a single list in Java?

Reverse a LinkedList Using Recursive ApproachStep 1: Split the list given into two parts - the first node and the rest of the linked list. Step 2: Invoke the reverseList() method for the remaining portion of the linked list. Step 3: Join the rest to the first. Step 4: Fix the head pointer.

How do I remove duplicates from LinkedList?

Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43.


2 Answers

Node next = tmp.next; while(tmp != null){ 

So what happens when tmp == null?

You almost got it, though.

Node before = null; Node tmp = head; while (tmp != null) {     Node next = tmp.next;     tmp.next = before;     before = tmp;     tmp = next; } head = before; 

Or in nicer (?) naming:

Node reversedPart = null; Node current = head; while (current != null) {     Node next = current.next;     current.next = reversedPart;     reversedPart = current;     current = next; } head = reversedPart; 

ASCII art:

        <__<__<__ __ : reversedPart    : head                  (__)__ __ __ head :   current:      >  >  > 
like image 143
Joop Eggen Avatar answered Sep 19 '22 06:09

Joop Eggen


public Node<E> reverseList(Node<E> node) {     if (node == null || node.next == null) {         return node;     }     Node<E> currentNode = node;     Node<E> previousNode = null;     Node<E> nextNode = null;      while (currentNode != null) {         nextNode = currentNode.next;         currentNode.next = previousNode;         previousNode = currentNode;         currentNode = nextNode;     }     return previousNode; } 
like image 40
Ranjeet Avatar answered Sep 23 '22 06:09

Ranjeet