Thread: Reverse a list in recursion

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    11

    Reverse a list in recursion

    Hello i'm trying to reverse a list using recursion i thought of a solution but there is one part of the solution i do not like,hoping i can get some ideas for it.
    Code:
    Node *rev(Node *head)
    {
    	Node *res, *tmp;
    	if (head->next == NULL)
    	{
    /*g is a global variable to save the new head,any ideas on how i can save the head without using a global variable?*/
    		g = head;
    		return head;
    	}
    	tmp = head;
    	res = rev(head->next);
    	res->next = tmp;
    	tmp->next = NULL;
    	return tmp;
    }

  2. #2
    Registered User
    Join Date
    Feb 2016
    Posts
    11
    I have solved it , i would still be glad to hear other opinions on the matter.
    Code:
    Node *rev(Node *head)
    {
    	Node *res,*ptr;
    	if (head->next == NULL)
    	{		
    		return head;
    	}
    	
    	res = rev(head->next);
    	ptr = res;
    	head->next = NULL;
    	while (ptr->next != NULL)
    	{
    		ptr = ptr->next;
    	}
    	ptr->next = head;
    	return res;
    }

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Can you keep track from both ends? That seems like it would be a lot faster.

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    11
    Quote Originally Posted by CodeSlapper View Post
    Can you keep track from both ends? That seems like it would be a lot faster.
    Sorry I don't understand what you mean, perhaps clarify ?

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    If you kept track of the tail and head pointer, you might be able to walk the list and swap the tail and head pointer. It might cut down on the complexity and number of operations.

    First go, swap head and tail, move both ptrs to the next one and keep going till they are both NULL. You might have to account for the when you have an even and odd number of elements if you do this.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    I'd make a helper function that takes the original list and the reversed list as parameters. Inside the helper function, if the original list has any elements in it, move the head from the original list to the head of the reversed list, and then call the helper function again from itself.

    Code:
    Node *rev_helper(Node *list, Node *reversed)
    {
        if (list) {
            Node *next = list->next;
            list->next = reversed;
            return rev_helper(next, list);
        }
        return reversed;
    }
    
    Node *rev(Node *list)
    {
        return rev_helper(list, NULL);
    }
    Untested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reverse string in recursion
    By Stark88 in forum C Programming
    Replies: 3
    Last Post: 02-28-2016, 01:49 PM
  2. reverse linked list
    By telmo_d in forum C Programming
    Replies: 7
    Last Post: 10-31-2015, 06:08 AM
  3. Reverse string using recursion
    By Satya in forum C Programming
    Replies: 1
    Last Post: 06-24-2015, 05:08 AM
  4. Reversing LinkList In Reverse Order Using Recursion
    By nickman in forum C Programming
    Replies: 6
    Last Post: 05-06-2014, 11:57 PM
  5. how to reverse items in linked list?
    By ilikeapplepie in forum C Programming
    Replies: 8
    Last Post: 04-09-2011, 11:15 PM

Tags for this Thread