1. 程式人生 > >leetcode 206 反轉鏈表 Reverse Linked List

leetcode 206 反轉鏈表 Reverse Linked List

tno style rev turn emp node image bubuko ext

技術分享圖片

只用了叠代,等會看一下大神的遞歸解法;

 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 public:
11     ListNode* reverseList(ListNode* head) {
12         if
(head==NULL||head->next==NULL) return head; 13 ListNode* pre,*cur; 14 pre=head;cur=head->next; 15 head->next=NULL; 16 while(cur!=NULL){ 17 ListNode* temp; 18 temp=cur->next; 19 cur->next=pre; 20 pre=cur;
21 cur=temp; 22 } 23 return pre; 24 } 25 };

leetcode 206 反轉鏈表 Reverse Linked List