Skip to content

Instantly share code, notes, and snippets.

@byron-perez
Created April 5, 2018 22:04
Show Gist options
  • Save byron-perez/f4bf0feb3583747cc206dde1fa063c7f to your computer and use it in GitHub Desktop.
Save byron-perez/f4bf0feb3583747cc206dde1fa063c7f 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)
{
Node* daeh;
daeh = (Node*)malloc(sizeof(Node));
daeh->next = NULL;
Node* head_trv = head;
while(head_trv)
{
Node* new_elem;
new_elem = (Node*)malloc(sizeof(Node));
new_elem->data = head_trv->data;
new_elem->next = daeh;
daeh = new_elem;
head_trv = head_trv->next;
}
Node* tail = daeh;
while(tail)
{
tail = tail->next;
}
return daeh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment