Created
October 15, 2012 00:50
-
-
Save nsfyn55/3890314 to your computer and use it in GitHub Desktop.
Reverse Singly Linked List C
This file contains hidden or 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
# include <stdio.h> | |
typedef struct Node { | |
struct Node *next; //stores the pointer to next | |
int value; | |
} Node; | |
void print_list(Node *root){ | |
while(root){ | |
printf("%d\n", root->value); | |
root = root->next; | |
} | |
} | |
Node* reverse(Node *current){ | |
Node *last = NULL; | |
Node *next = NULL; | |
Node *ret; | |
while(current){ | |
next = current->next; // next points to 2 | |
current->next = last; // 1 points to null | |
last = current; // last points to 1 | |
ret = current; | |
current = next; // current points to 2 | |
} | |
return ret; | |
} | |
int main(){ | |
Node n1 = {0, 1}; | |
Node n2 = {&n1, 2}; | |
Node n3 = {&n2, 3}; | |
Node *root = &n3; | |
//print_list(root); | |
Node *result = reverse(&n3); | |
print_list(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment