Thread: Reversing a Linked List

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    228

    Reversing a Linked List

    I've been trying to reverse a linked list but I'm not sure if this piece of code that I made does what I think it does. The process in my mind is, create a empty node that will be returned as our reverse linked list. Create a temp node to the head of the original node and have
    Code:
    temp->next = reverseList
    . Finally, have
    Code:
    reverseList = temp
    and
    Code:
     head = head->next
    These instructions are to be repeated until head equals null

    What this function should do is take the linked list and gradually do this

    Code:
    {}, {2,3,5,6,NUll}    
    {2, NULL}, {3,5,6,NULL}     
    {3, 2, NULL}, {5,6.NULL}    
    {5,3,2,NULL}, {6,NULL}   
    {6,5,3,2, NULL}, {}.
    At this point head should be empty.

    Code:
    Node *reverse(Node *head)
    {
        if(head == NULL)
        {
            return NULL;
        }
        else if(head->next == NULL)
        {
            return head;
        }
        
        Node *reverseList = NULL;
        
        while(head != NULL)
        { 
           Node *temp = head;
           temp->next = reverseList;
           reverseList = temp;
           head = head->next;
        }
        
        return reverseList;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It looks good to me.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    228
    Cool but the website that has this challenge said that it produces the wrong answer. HackerRank

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You need to save head->next each time since you're changing it.
    Code:
    Node *reverse(Node *head) {
        if (!head || !head->next) return head;
        Node *revList = NULL, *next = NULL;
        for ( ; head; head = next) { 
           next = head->next;
           head->next = revList;
           revList = head;
        }
        return revList;
    }

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Ah, I missed that temp is an alias for head somehow. Sorry.

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    228
    Damn it that was the problem. XD thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing The Linked List by reversing the links only
    By bitanbasak in forum C Programming
    Replies: 7
    Last Post: 08-25-2016, 09:24 AM
  2. Reversing a linked List
    By 00111_Bim in forum C Programming
    Replies: 4
    Last Post: 04-09-2014, 11:49 PM
  3. Reversing a linked list...
    By csharp100 in forum C Programming
    Replies: 2
    Last Post: 11-09-2010, 12:37 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. reversing a singly linked list
    By galmca in forum C Programming
    Replies: 6
    Last Post: 02-07-2005, 10:25 AM

Tags for this Thread