List Reverse( List L ) { structNode* head =NULL; structNode* current = L; while (current != NULL) { structNode* temp = current->Next; if (!head) { head = current; head->Next = NULL; current = temp; continue; } current->Next = head; head = current; current = temp; } return head; }
解法二
解法描述
所谓反转链表无非是两两结点之间箭头的变换
对比前后的图像就能够很轻易地得出解决方案
具体代码
1 2 3 4 5 6 7 8 9 10 11
List Reverse( List L ) { structNode* prev =NULL; structNode* current = L; while (current != NULL) { structNode* temp = current->Next; current->Next = prev; prev = current; current = temp; } return prev; }