1

Please accept my apologies first, but I could not reverse my Linked List in java..

I have class and inner class:

MyList{

class Element{
private Element next;

public Element getNext(){return next;}
}

 public void reverseMyList(Element curr) {

        if (curr.next == null) {
            head = curr.next;
            return;
        }
        reverseMyList(curr.next);
        while (curr.next != null) {
            curr.next.next = curr.next;
            curr.next = null;
        }
}//:~

I need to reverse my List, I am using method reverseMyList, which needs Element curr. If my way of thinking in this case is correct ?

Thank you in advance!

Iana
  • 458
  • 3
  • 7
  • 18
  • 1
    Is there supposed to be something *in* these lists? I don't see anywhere to put elements. – user2357112 Dec 12 '13 at 15:43
  • is recursion mandatory ? homework ? – PeterMmm Dec 12 '13 at 15:45
  • If this is for homework, you may find http://stackoverflow.com/questions/354875/reversing-a-linked-list-in-java-recursively or http://stackoverflow.com/questions/12943720/reversing-a-singly-linked-list-iteratively useful. – lebolo Dec 12 '13 at 15:45
  • 1
    If you need a hint, you don't need any recursive calls to reverse your list. You just iterate over your list, and make sure that each element's `next` points back to what use to be it's parent – Sam I am says Reinstate Monica Dec 12 '13 at 15:46
  • What is `head` your example is not correct. Even your `Element` is wrong – nachokk Dec 12 '13 at 15:48
  • A linked list doesn't really have a predefined order. It is just the convention to follow the chain in a specific direction (by accessing `Node.next`). However, you could simply walk in the other direction (since it is a loop). – Martijn Courteaux Dec 12 '13 at 15:51

3 Answers3

0

Since this kinda looks like homework, I'm not going to lay out the entire solution here, but I will explain how you should conceptually do it.

Imagine that you have 2 linked lists. You have your input list that you need to reverse, and you have an empty one.

If you keep taking the first element off of the original list, and keep putting that on the front of the new list until your original list is empty, than your new list will be what the original was, except reversed.

  • Pushing everything into a stack and then popping into a new list would also work. Implementation left as an exercise for the reader. – Mike G Dec 12 '13 at 15:55
0
public static void Reverse(Element element)
    {
        Element current = element;
        Element next = current.Next;
        Element nextToNext;

        var first = current;

        while (next != null && next.Next != null)
        {
            nextToNext = next.Next;

            next.Next = current;

            current = next;

            next = nextToNext;
        }

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

        first.Next = null;
    }
Shaggy
  • 63
  • 5
0

the method you are looking for already exist in package java.utils:

Collections.reverse(mylist);

this method will change the order of element direcly inside your list and you dont need to instance a new list-object... here you can find more specified documentation

Mike D3ViD Tyson
  • 1,701
  • 1
  • 21
  • 31