www

导航

单链表反转

 1 ListNode* Reverse(ListNode *head)
 2 {
 3     if (head == nullptr || head->next == nullptr)
 4         return head;
 5 
 6     ListNode *p = head, *q = head->next;
 7     p->next = nullptr;
 8     while (q->next != nullptr)
 9     {
10         ListNode *temp = q->next;
11         q->next = p;
12         p = q;
13         q = temp;
14     }
15     q->next = p;
16     return q;
17 }

 

posted on 2017-09-19 09:51  www_practice  阅读(119)  评论(0编辑  收藏  举报