Single-linked list is the classic list operation algorithm, single-linked list of the algorithm idea is to reverse the list arrow (assuming that the next pointer is an arrow), the so-called change chain, the chain process is as follows.
逆序前:head-->......prev-->cur-->next-->......->NULL逆序后:NULL<--......prev<--cur<--next<--......head
Algorithmic logic:
1. Empty list or only one element, return the original linked list head.
2. Define 3 pointers prev, cur, next, when initialized, Prev points to head,cur next to Prev, next to cur, when cur non-empty whole moves prev, cur, next, and simultaneously changes the chain operation.
3. Point the head's next to null, and return to the linked list header cur after the reverse order.
Implementation algorithm See REVERSE_LINK.C.
/*file: REVERSE_LINK.C*/typedef struct Node {intValue struct node*next;} node_t;node_t*reverse_link(node_t*head) {node_t*prev,*cur,*next;if(NULL = = Head | | NULL = = head->Next)returnHead prev = head; Cur = prev->Next; while(cur) {Next= cur->Next; Cur->Next= prev; prev = cur; Cur =Next; } head->Next= NULL;returnPrev }
Linked List-reverse list of single-linked lists