206. 反转链表

不羁的心 提交于 2020-03-22 18:10:41
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution 
10 {
11 public:
12     ListNode* reverseList(ListNode* head) 
13     {
14         ListNode* new_head = NULL;
15         while(head)
16         {
17             ListNode* temp = head->next;
18             head->next = new_head;
19             new_head = head;
20             head = temp;
21         }
22         return new_head;
23     }
24 };

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!