3

I'm having problems trying to write a reverse recursive method for a LinkedList class I created in C#. The LinkedList has 2 pointers in it one for the head and the other for the tail:

public class Node
{
    public object data;
    public Node next;
    public Node(object Data)
    {
        this.data = Data;
    }
}

public class LinkedList
{
    Node head;
    Node tail;
    public void Add(Node n)
    {
        if (head == null)
        {
            head = n;
            tail = head;
        }
        else
        {
            tail.next = n;
            tail = tail.next;
        }
    }

Now, the recursive reverse function goes like this:

    public void reverse_recursive()
    {
        Node temp_head = head;
        if (temp_head == tail)
        {

            return;
        }

        while (temp_head != null)
        {
            if (temp_head.next == tail)
            {
                tail.next = temp_head;
                tail = temp_head;
                reverse_recursive();
            }
            temp_head = temp_head.next;
        }
    }

I'm having 2 troubles with it: first, a logic problem, I know that head doesn't point to the first node after the reverse. The second problem is that i probably do something wrong with the null pointer so the program crashes.

I also give you the main program:

class Program
{
    static void Main(string[] args)
    {
        LinkedList L = new LinkedList();
        L.Add(new Node("first"));
        L.Add(new Node("second"));
        L.Add(new Node("third"));
        L.Add(new Node("forth"));
        L.PrintNodes();
        L.reverse_recursive();
        L.PrintNodes();
        Console.ReadLine();
    }
}

Thank you for helping!!

3
  • 4
    Might I ask you why you haven't ever awarded a single question? Commented Jul 26, 2013 at 16:31
  • before helping i just want to check, do you need to use your own linked list class? there is a built in class: msdn.microsoft.com/en-us/library/he2s3bh7.aspx Commented Jul 26, 2013 at 16:31
  • You are checking (temp_head != null) in the while, but within temp_head.next may be null so if (temp_head.next == tail) may cause violation. Commented Jul 26, 2013 at 16:32

6 Answers 6

3
public void Reverse()
{
    this.Reverse(this.head);
}

private void Reverse(Node node)
{
    if (node != null && node.next != null)
    {
        // Create temporary references to the nodes, 
        // because we will be overwriting the lists references.
        Node next = node.next;
        Node afterNext = node.next.next;
        Node currentHead = this.head;

        // Set the head to whatever node is next from the current node.
        this.head = next;

        // Reset the next node for the new head to be the previous head.
        this.head.next = currentHead;

        // Set the current nodes next node to be the previous next nodes next node :)
        node.next = afterNext;

        // Keep on trucking.
        this.Reverse(node);
    }
    else
    {
        this.tail = node;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
        public void reverse()
        {
            reverse_recursive(tail);

            Node tmp = tail;
            tail = head;
            head = tmp;
        }

        public void reverse_recursive(Node endNode)
        {
            Node temp_head = head;
            if (temp_head == endNode)
            {
                return;
            }

            while (temp_head != null)
            {
                if (temp_head.next == endNode)
                {
                    break;
                }
                temp_head = temp_head.next;
            }

            endNode.next = temp_head;
            temp_head.next = null;
            reverse_recursive(temp_head);
        }

See also this

Comments

0

Another option over here.

class Program{
    static void Main(string[] args)
    {
        LinkedList L = new LinkedList();
        L.Add(new Node("first"));
        L.Add(new Node("second"));
        L.Add(new Node("third"));
        L.Add(new Node("forth"));
        L.PrintNodes();
        L.reverse_recursive();
        Console.WriteLine("---------------------");
        L.PrintNodes();
        Console.ReadLine();
    }
}

public class Node
{
    public object data;
    public Node next;
    public Node(object Data)
    {
        this.data = Data;
    }
}

public class LinkedList
{
    Node head;
    Node tail;
    public void Add(Node n)
    {
        if (head == null)
        {
            head = n;
            tail = head;
        }
        else
        {
            tail.next = n;
            tail = tail.next;
        }

    }

    public void PrintNodes()
    {
        Node temp = head;

        while (temp != null)
        {
            Console.WriteLine(temp.data);
            temp = temp.next;
        }
    }


    private LinkedList p_reverse_recursive(Node first)
    {
        LinkedList ret;
        if (first.next == null)
        {
            Node aux = createNode(first.data);
            ret = new LinkedList();
            ret.Add(aux);
            return ret;
        }
        else
        {
            ret = p_reverse_recursive(first.next);
            ret.Add(createNode(first.data));
            return ret;
        }

    }

    private Node createNode(Object data)
    {
        Node node = new Node(data);
        return node;
    }

    public void reverse_recursive()
    {

        if (head != null)
        {
            LinkedList aux = p_reverse_recursive(head);
            head = aux.head;
            tail = aux.tail;
        }


    }
}

Hope it helps

Comments

0

A second variant

private void p_reverse_recursive2(Node node)
    {
        if (node != null)
        {
            Node aux = node.next;
            node.next = null;
            p_reverse_recursive2(aux);
            if (aux != null)
                aux.next = node;
        }

    }

    public void reverse_recursive()
    {

        if (head != null)
        {               
            Node aux = head;
            head = tail;
            tail = aux;
            p_reverse_recursive2(tail);
        }


    }

Comments

0

Variation on a theme...

public Node Reverse(Node head)
{
    if(head == null)
    {
        return null;
    }

    Node reversedHead = null;
    ReverseHelper(head, out reversedHead);
    return reversedHead;
}

public Node ReverseHelper(Node n, out Node reversedHead)
{
    if(n.Next == null)
    {
        reversedHead = n;
        return n;
    }

    var reversedTail = ReverseHelper(n.Next, out reversedHead);
    reversedTail.Next = n;
    n.Next = null;
    return n;       
    }   
}

Comments

0

I was just playing with similar brain teaser with the only difference that LinkedList class only has a definition for head and all the rest of the nodes are linked there. So here is my quick and dirty recursive solution:

public Node ReverseRecursive(Node root)
        {
            Node temp = root;
            if (root.next == null)
                return root;
            else
                root = ReverseRecursive(root.next);
            temp.next = null;
            Node tail = root.next;
            if (tail == null)
                root.next = temp;
            else
                while (tail != null)
                {
                    if (tail.next == null)
                    {
                        tail.next = temp;
                        break;
                    }
                    else
                        tail = tail.next;
                }
            return root;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.