Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
Created October 15, 2012 00:50
Show Gist options
  • Save nsfyn55/3890314 to your computer and use it in GitHub Desktop.
Save nsfyn55/3890314 to your computer and use it in GitHub Desktop.
Reverse Singly Linked List C
# 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