-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19&&92&&206.cpp
58 lines (51 loc) · 1.29 KB
/
19&&92&&206.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
using namespace std;
struct ListNode{
ListNode* next,
int val;
ListNode(int val):next(nullptr),val(val){}
};
ListNode* removeNthFromEnd(ListNode* head, int n){
ListNode** terminal=&head,*iter=head;
for(int i=1;i!=n;++i)iter=iter->next;
while(iter->next!=nullptr){
terminal=&((*terminal)->next);
iter=iter->next;
}
*terminal=*(iterminal)->next;
return head;
}
ListNode* reverse(ListNode* head){
if(head==nullptr || head->next==nullptr)return head;
auto l=reverse(head->next);
head->next->next=head;
head->next=nullptr;
return l;
}
ListNode* reverseIter(ListNode* head){
auto pre=nullptr;
while(head!=nullptr){
auto t=head->next;
head->next=pre;
pre=head;
head=t;
}
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(head==nullptr||head->next==nullptr||m>=n) return head;
ListNode dummy(-1);dummy.next=head;
int i=0;
auto left_l=&dummy;
for(;i<m-1;++i)left_l=left_l->next;
auto cur=left_l->next,left_r=cur;ListNode*prev=nullptr;
++i;
for(;cur&&i<=n;++i){
auto tmp=cur->next;
cur->next=prev;
prev=cur;
cur=tmp;
}
left_l->next=prev;left_r->next=cur;
return dummy.next;
}
int main(){
}