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 and 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;
}