Skip to content

Instantly share code, notes, and snippets.

@PraveenKishore
Created June 16, 2017 10:16
Show Gist options
  • Select an option

  • Save PraveenKishore/6e573a9fc84b3914f35401e29fe3c880 to your computer and use it in GitHub Desktop.

Select an option

Save PraveenKishore/6e573a9fc84b3914f35401e29fe3c880 to your computer and use it in GitHub Desktop.
Reverse a linked list
/* https://www.hackerrank.com/challenges/reverse-a-linked-list
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)
{
// Complete this method
Node *next;
Node *current = head;
Node *previous = NULL;
while(current != NULL) {
next = current->next;
current->next = previous;
previous = current;
current = next;
}
return previous;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment