Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 <= m <= n <= length of list.
Solution: in-place & one-pass.
1 class Solution { 2 public: 3 ListNode *reverseBetween(ListNode *head, int m, int n) { 4 ListNode dummy(0); 5 ListNode* pre = &dummy; 6 dummy.next = head; 7 for(int i = 0; i < m-1; i++) { 8 pre = pre->next; 9 } 10 ListNode* cur = pre->next; 11 for(int i = 0; i < n-m; i++) { 12 ListNode* move = cur->next; 13 cur->next = move->next; 14 move->next = pre->next; 15 pre->next = move; 16 } 17 return dummy.next; 18 } 19 };