Skip to content

Instantly share code, notes, and snippets.

@melastmohican
Created November 1, 2017 21:31
Show Gist options
  • Select an option

  • Save melastmohican/abcde2fea111dc2fba06c5ff7d9c7642 to your computer and use it in GitHub Desktop.

Select an option

Save melastmohican/abcde2fea111dc2fba06c5ff7d9c7642 to your computer and use it in GitHub Desktop.
/*
Reverse a linked list and return pointer to the head
The input list will have at least one element
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Reverse(Node *head)
{
if(head == NULL) return NULL;
Node *current = head;
Node *prev = NULL;
Node *next = NULL;
while(current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment