翻转名单
翻转列表前完成。在这里,看到一个更简单的实现
ListNode* Reverse(ListNode *head) { ListNode *reHead = NULL; ListNode *prev = NULL; ListNode *Node = head; while(Node != NULL) { ListNode *next = Node->next; if(next == NULL) reHead = Node; Node->next = prev; prev = Node; Node = next; } return reHead; }
还有
ListNode* reverse(ListNode *head){ if(head == NULL || head->next == NULL ) return head; ListNode* tmp = reverse(head->next); head->next->next = head; head->next = NULL; return tmp; }
版权声明:本文博主原创文章,博客,未经同意不得转载。