-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseBetween.cpp
43 lines (37 loc) · 1.11 KB
/
reverseBetween.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/********************************************************************
Copyright : CNIC
Author : LiuYao
Date : 2017-9-1
Description : leetcode 92. Reverse Linked List II
********************************************************************/
#include <cstddef>
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* newHead = new ListNode(0);
newHead -> next = head;
ListNode* l_end = newHead, *m_start = NULL, *m_end = NULL, *r_start = NULL, *cur = newHead -> next, *last = newHead, *next;
int idx = 1;
while(cur){
next = cur -> next;
if(idx == m - 1) l_end = cur;
if(idx == m) m_start = cur;
if(idx > m && idx <= n){
cur -> next = last;
}
if(idx == n) m_end = cur;
if(idx == n + 1) r_start = cur;
last = cur;
cur = next;
idx++;
}
if(m_end)
l_end -> next = m_end;
if(m_start)
m_start -> next = r_start;
return newHead -> next;
}