Reverse Linked List II
2015-01-23 12:10
344 查看
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *reverseBetween(ListNode *head, int m, int n) { if(m==n) { return head; } m=m-1; n=n-1; ListNode* start=NULL; ListNode* end=NULL; ListNode* pre=NULL; ListNode* cur=head; ListNode* next=head->next; for(int i=0;i<m;++i) { pre=cur; cur=next; next=next->next; } start=pre; for(int i=0;i<(n-m);++i) { cur->next=pre; pre=cur; cur=next; next=next->next; } end=cur->next; cur->next=pre; if(start==NULL) { head->next=end; return cur; } else { start->next->next=end; start->next=cur; return head; } } };
相关文章推荐
- leetcode之Reverse Linked List II
- LeetCode之Reverse Linked List II
- leetcode——92——Reverse Linked List II
- [Leetcode] 92. Reverse Linked List II
- 92. Reverse Linked List II 等题
- Reverse Linked List II--LeetCode
- [leedcode 92] Reverse Linked List II
- leetcode 092 Reverse Linked List II
- leetcode[92] Reverse Linked List II
- LeetCode "Reverse Linked List II"
- 206. Reverse Linked List&&92. Reverse Linked List II
- Reverse Linked List II
- LeetCode:Reverse Linked List II 解法
- Reverse Linked List II - LeetCode
- leetcode92. Reverse Linked List II
- 92. Reverse Linked List II
- Reverse Linked List II 解答
- Reverse Linked List II
- Reverse Linked List II
- Leetcode-Reverse Linked List II