Reversing a singly linked list?

Viewed 104

This is the code I was given for the Singly Linked List, however I am struggling on completing the Reverse function. This was the code and my attempt at the reverse function. I keep getting 2 errors that say "Undeclared variable: node" and "imcompatible types: Node cannot be converted to Linkedlist".

class LinkedList
{
    Node head;
    Node current;
    Node previous;

    public Object Get()
    {
        return current != null ? current.GetData() : null;
    }

    public void Next()
    {
        if (current != null)
        {
            previous = current;
            current = current.next;
        }
    }

    public void Head()
    {
        previous = null;
        current = head;
    }

    public void Insert(Object data)
    {
        Node node = new Node(data);
        node.next = current;

        if (current == head)
            head = node;
        else
            previous.next = node;

        current = node;
    }

    public void Remove()
    {
        if (current == null)
            throw new RuntimeException("Invalid position to remove");

        if (current == head)
            head = current.next;
        else
            previous.next = current.next;

        current = current.next;
    }

    public void Print()
    {
        for (Head(); Get() != null; Next())
            System.out.println(Get());
    }

    public LinkedList Reverse()
    {
        Node previous = null;  
        Node current = node;  
        Node forward;  

        while (current != null) 
        {  
            forward = current.next;  
            current.next = previous;  
            previous = current;  
            current = forward;  
        }  
    return previous;  
    }  

}

There is also class Node: class Node { // Public reference to next node public Node next;

    // Private data field
    Object data;

    Node(Object data)
    {
        this.data = data;
    }

    public Object GetData()
    {
        return data;
    }
}

And this is the main function: class Test { public static void main(String args[]) { // creating a singly linked list LinkedList linked_list = new LinkedList();

        // adding node into singly linked list 
        linked_list.Insert(Integer.valueOf(10));
        linked_list.Next();
        linked_list.Insert(Integer.valueOf(11));
        linked_list.Next();
        linked_list.Insert(Integer.valueOf(12));
        
        // printing a singly linked 
        linked_list.Print();
        
        // reversing the singly linked list 
        linked_list.Reverse(); 
        
        // printing the singly linked list again 
        linked_list.Print(); 
    } 
}
1 Answers

Here is a simple solution:

public class ListReverser {
    public static Node<Integer> reverse(Node head) {
        Node current = head;
        while(current.getNext() != null) {
            Node next =  current.getNext();
            current.setNext(next.getNext());
            next.setNext(head);
            head = next;
        }
        return head;
    }
}
Related