Linked List

題目

  • 19 Remove Nth Node From End of List (Medium)

  • 21 Merge Two Sorted Lists

  • 23 Merge k Sorted Lists

  • 141 Linked List Cycle (Easy)

  • 142 Linked List Cycle II (Medium)

  • 160 Intersection of Two Linked Lists

  • 876 Middle of the Linked List (Easy)

  • 2 Add Two Numbers (Medium)

  • 25 Reverse Nodes in k-Group (Hard)

  • 83 Remove Duplicates from Sorted List (Easy)

  • 206 Reverse Linked List(Easy)

  • 92 Reverse Linked List II (Medium)

  • 234 Palindrome Linked List (Easy)

  • 203 Remove Linked List Elements

  • 83 Remove Duplicates from Sorted List

  • *82 Remove Duplicates from Sorted List II

補充

  • 2095 Delete the Middle Node of a Linked List (Medium)
  • 2130 Maximum Twin Sum of a Linked List (Medium)
  • 148 Sort List (Medium)

implement

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include<iostream>
using namespace std;

class ListNode{
private:
int val;
ListNode *next;

public:
ListNode():val(0), next(nullptr){};
ListNode(int x):val(x), next(nullptr){};
friend class LinkedList;
};

class LinkedList{

private:
ListNode *first;

public:
LinkedList():first(){};
void Print();
void Push_back(int x);
void Push_front(int x);
void Pop_back();
void Pop_front();
void Delete(int x);
void Clear();
void Reverse();
};

void LinkedList::Print(){
if(first==nullptr){
std::cout<<"the list is empty"<<std::endl;
return;
}
ListNode* cur = first;

while(cur){
std::cout<<cur->val<<" ";
cur = cur->next;
}
std::cout <<std::endl;
}

void LinkedList::Push_back(int x){
ListNode* newnode = new ListNode(x);
if(first==nullptr){
first = newnode;
return ;
}
ListNode* cur = first;

while(cur->next){
cur = cur->next;
}
cur->next = newnode;
}

void LinkedList::Push_front(int x){
ListNode* newnode = new ListNode(x);
if(first==nullptr){
first = newnode;
return ;
}

newnode->next = first;
first = newnode;
}

void LinkedList::Pop_back(){
if(first==nullptr){
std::cout <<"the list is empty"<<std::endl;
return ;
}
ListNode* cur = first;
if(cur->next==nullptr){
ListNode * delnode = cur;
cur = cur->next;
delete delnode;
first = nullptr;
return;
}
ListNode* post = cur->next;
while(post->next){
post = post->next;
cur = cur->next;
}
cur->next = nullptr;
delete post;
}

void LinkedList::Pop_front(){
if(first==nullptr){
std::cout <<"the list is empty"<<std::endl;
return ;
}
ListNode* cur = first;
// if(cur->next==nullptr){
// ListNode * delnode = cur;
// cur = cur->next;
// delete delnode;
// first = cur;
// return;
// }
ListNode * delnode = cur;
cur = cur->next;
delete delnode;
first = cur;
}

void LinkedList::Delete(int x){
if(first==nullptr){
std::cout <<"the list is empty"<<std::endl;
return ;
}
ListNode * cur = first;
if(cur->val==x){
ListNode * delnode = cur;
cur = cur->next;
delete delnode;
first = cur;
return;
}
ListNode * post = cur->next;
while(post){
if(post->val==x){
break;
}
cur = cur->next;
post = post->next;
}
if(post==nullptr) return;
ListNode * delnode = post;
cur->next=post->next;
delete delnode;


}

void LinkedList::Clear(){
if(first==nullptr){
std::cout <<"the list is empty"<<std::endl;
return;
}
if(first->next==nullptr){
ListNode * delnode = first;
first = first->next;
delete delnode;
}
// ListNode * cur = first; // bad idea
while(first){
ListNode * delnode = first;
first = first->next;
delete delnode;
}
}
void LinkedList::Reverse(){

if(first==nullptr || first->next==nullptr) return;

ListNode *pre = nullptr;
ListNode *cur = first;
ListNode *post = cur->next;

while(post){
cur->next = pre;
pre = cur;
cur = post;
post = post->next;
}
cur->next = pre;
first = cur;
}
int main(){

LinkedList l;
l.Print();


l.Push_back(5);
l.Push_back(8);
l.Print();
l.Push_front(3);
l.Push_front(1);
l.Print();
// l.Pop_front();
// l.Pop_front();
// l.Pop_back();
// l.Pop_back();
l.Print();
l.Reverse();
l.Print();
l.Delete(3);
l.Print();
l.Clear();
l.Print();


return 0;

}