Created
April 5, 2018 22:04
-
-
Save byron-perez/f4bf0feb3583747cc206dde1fa063c7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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