Created
May 20, 2012 17:54
-
-
Save vo/2758919 to your computer and use it in GitHub Desktop.
Reversing a Linked List Using Recursion
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
#include <stdlib.h> | |
#include <stdio.h> | |
struct Link { | |
int value; | |
struct Link * next; | |
}; | |
typedef struct Link node; | |
// add a node to the beginning of list | |
// return the pointer to the new beginning | |
node * push(node * head, int value) { | |
node * l = malloc(sizeof(node)); | |
l->value = value; | |
l->next = head; | |
return l; | |
} | |
// print the list starting from head | |
void print(node * head) { | |
node * i = head; | |
printf("{"); | |
while(i != NULL) { | |
if(i == head) | |
printf("%d",i->value); | |
else | |
printf(",%d",i->value); | |
i = i->next; | |
} | |
printf("}\n"); | |
} | |
// reverse the list | |
node * reverse (node * head) { | |
if(head->next == NULL) | |
return head; | |
else { | |
node * newhead = reverse(head->next); | |
head->next->next = head; | |
head->next = NULL; | |
return newhead; | |
} | |
} | |
int main() { | |
// make a linked list | |
node * head = NULL; | |
head = push(head, 5); | |
head = push(head, 4); | |
head = push(head, 3); | |
head = push(head, 2); | |
head = push(head, 1); | |
// print the list, reverse it, and print again | |
print(head); | |
head = reverse(head); | |
print(head); | |
} |
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
public class Link { | |
int value; | |
Link next; | |
public Link(int value) { | |
this.value = value; | |
this.next = null; | |
} | |
// print the list. | |
public static void print(Link first) { | |
Link i = first; | |
System.out.print("{"); | |
while(i != null) { | |
if (i != first) | |
System.out.print(","); | |
System.out.print(i.value); | |
i = i.next; | |
} | |
System.out.println("}"); | |
} | |
// reverse the list. return the new head. | |
public static Link reverse(Link l) { | |
if(l.next == null) | |
return l; | |
else { | |
Link head = reverse(l.next); | |
l.next.next = l; | |
l.next = null; | |
return head; | |
} | |
} | |
public static void main(String[] args) { | |
// make a linked list | |
Link l1 = new Link(1); | |
Link l2 = new Link(2); | |
Link l3 = new Link(3); | |
Link l4 = new Link(4); | |
Link l5 = new Link(5); | |
l1.next = l2; | |
l2.next = l3; | |
l3.next = l4; | |
l4.next = l5; | |
Link head = l1; | |
// print the list, reverse, and print again | |
print(head); | |
head = reverse(head); | |
print(head); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you explain how reversing linked list working ?