L
lovecreatesbea...
Reverse a single directional non-circle list. For example:
p | p
| | |
+-+-+ +---+ +---+ +---+ | +---+ +---+ +---+ +-+-
+
| 1 | -> | 2 | -> | 3 | -> | 4 | | | 1 | <- | 2 | <- | 3 | <- | 4
|
+---+ +---+ +---+ +---+ | +---+ +---+ +---+ +---
+
Comments are welcome.
struct node {int data; struct node next;};
struct node list_rvs(struct node p)
{
struct node p1 = p, p2 = p->next, p3;
while (p2){
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
p->next = 0;
p = p1;
return p;
}
p | p
| | |
+-+-+ +---+ +---+ +---+ | +---+ +---+ +---+ +-+-
+
| 1 | -> | 2 | -> | 3 | -> | 4 | | | 1 | <- | 2 | <- | 3 | <- | 4
|
+---+ +---+ +---+ +---+ | +---+ +---+ +---+ +---
+
Comments are welcome.
struct node {int data; struct node next;};
struct node list_rvs(struct node p)
{
struct node p1 = p, p2 = p->next, p3;
while (p2){
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
p->next = 0;
p = p1;
return p;
}