-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListNode.cpp
356 lines (313 loc) · 5.69 KB
/
ListNode.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <math.h>
#include <stack>
using namespace std;
//链表定义
typedef struct ListNode
{
int data;
ListNode *next;
}ListNode;
// 创建链表
ListNode* CreateList(int num)
{
if(num<=0)
return NULL;
ListNode *head = NULL,*s = NULL, *p = NULL;
for(int i=0;i<num;i++)
{
p = (ListNode*)malloc(sizeof(ListNode));
int tmp_data;
cin>>tmp_data;
p->data = tmp_data;
if(head==NULL)
{
head = p;
s = p;
}
else
{
s->next = p;
s = p;
}
}
s->next = NULL;
return head;
}
// 输出链表
void PrintList(ListNode *head)
{
if(NULL==head)
return;
while(head)
{
cout<<head->data;
head = head->next;
}
cout<<endl;
}
// 逆向输出链表,利用栈
void PrintListReverse(ListNode *head)
{
if(head==NULL)
return ;
stack<ListNode*> st;
while(head!=NULL)
{
st.push(head);
head = head->next;
}
while(!st.empty())
{
cout<<st.top()->data;
st.pop();
}
cout<<endl;
return;
}
// 逆向输出链表,利用递归
void PrintListReverseRecursive(ListNode *head)
{
if(head!=NULL)
{
if(head->next!=NULL)
PrintListReverseRecursive(head->next);
cout<<head->data;
}
// cout<<endl;
return;
}
// 链表逆序
ListNode* ReverseList(ListNode *head)
{
ListNode *ReverseHead = NULL;
ListNode *pNode = head;
ListNode *pPrev = NULL;
ListNode *pNext = NULL;
while(pNode!=NULL)
{
pNext = pNode->next;
if(pNext == NULL)
ReverseHead = pNode;
pNode->next = pPrev;
pPrev = pNode;
pNode = pNext;
}
return ReverseHead;
}
// 合并两个有序列表
ListNode* MergeList(ListNode *head1, ListNode*head2)
{
ListNode *head = NULL;
if(head1==NULL && head2==NULL)
return NULL;
if(head1==NULL && head2!=NULL)
return head2;
if(head1!=NULL && head2==NULL)
return head1;
if(head1->data<head2->data)
{
head = head1;
head->next = MergeList(head1->next,head2);
}
else
{
head = head2;
head->next = MergeList(head1,head2->next);
}
return head;
}
// 判断链表是否有环儿,有环儿的话返回相遇节点
ListNode* CheckLoop(ListNode *head)
{
ListNode *slow = head;
ListNode *fast = head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(fast == slow)
return fast;
}
return NULL;
}
//求链表环儿长度
int LoopLength(ListNode *head)
{
int length = 0;
ListNode *slow = head;
ListNode *fast = head;
bool again = false;
bool begin = false;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(slow==fast && begin==false)
{
begin = true;
again = true;
}
if(slow==fast && again==true)
break;
if(again==true)
length++;
}
return length;
}
// 求链表环的入口节点
ListNode* EntryNodeOfLoop(ListNode *head)
{
ListNode *meetNode = CheckLoop(head);
if(NULL == meetNode)
return NULL;
ListNode *pNode1 = meetNode;
int loopNum = 1;
//先求环的长度
while(pNode1->next != meetNode)
{
++loopNum;
pNode1 = pNode1->next;
}
pNode1 = head;
for(int i=0;i<loopNum;i++)
pNode1 = pNode1->next;
ListNode *pNode2 = head;
while(pNode1!=pNode2)
{
pNode1 = pNode1->next;
pNode2 = pNode2->next;
}
return pNode1;
}
// 输出该链表中倒数第k个结点
ListNode* FindKthToTail(ListNode *head, unsigned int k)
{
if(head==NULL || k<=0)
return NULL;
ListNode *pAhead = head;
ListNode *pBehind = head;
//先走k-1步
for(int i=0;i<k-1;i++)
{
if(pAhead->next!=NULL)
pAhead = pAhead->next;
else
return NULL;
}
while(pAhead->next!=NULL)
{
pAhead = pAhead->next;
pBehind = pBehind->next;
}
return pBehind;
}
// 给定一个节点,在链表中删除该节点
void DeleteListNode(ListNode **head, ListNode *deleteNode)
{
if(!head||!deleteNode)
return;
// 链表只有一个节点的情况
if(*head == deleteNode)
{
delete deleteNode;
deleteNode = NULL;
*head = NULL;
}
// 链表有多个节点,删除节点不是链表的最后一个节点
else if(deleteNode->next!=NULL)
{
ListNode *pNext = deleteNode->next;
deleteNode->data = pNext->data;
deleteNode->next = pNext->next;
delete pNext;
pNext = NULL;
}
// 链表有多个节点,删除节点是链表的最后一个节点
else
{
ListNode *pNode = *head;
while(pNode->next!=deleteNode)
pNode = pNode->next;
pNode->next = NULL;
delete deleteNode;
deleteNode = NULL;
}
return ;
}
// 输入两个链表,找出它们的第一个公共结点
int GetListLength(ListNode *head)
{
int listLength = 0;
ListNode *pNode = head;
while(pNode!=NULL)
{
++listLength;
pNode = pNode->next;
}
return listLength;
}
ListNode* FindFirstCommonNode(ListNode *head1, ListNode *head2)
{
int len1 = GetListLength(head1);
int len2 = GetListLength(head2);
int dif = 0;
ListNode *longHead = NULL;
ListNode *shortHead = NULL;
if(len1>len2)
{
dif = len1-len2;
longHead = head1;
shortHead = head2;
}
else
{
dif = len2-len1;
longHead = head2;
shortHead = head1;
}
for(int i=0;i<dif;i++)
longHead = longHead->next;
while(longHead!=NULL && shortHead!=NULL && longHead!=shortHead)
{
longHead = longHead->next;
shortHead = shortHead->next;
}
return longHead;
}
int Add(int a, int b)
{
int sum,carry;
do
{
sum = a^b;
carry = (a&b)<<1;
a = sum;
b = carry;
}while(carry!=0);
return a;
}
int main(int argc, char const *argv[])
{
/* code */
cout<<Add(3,-33)<<endl;
// ListNode *list = CreateList(5);
// PrintList(list);
// ListNode *k = FindKthToTail(list,10);
// if(k==NULL)
// cout<<"error"<<endl;
// else
// cout<<k->data<<endl;
// ListNode *list2 = ReverseList(list);
// PrintList(list2);
// PrintList(list);
// ListNode *list3 = CreateList(5);
// PrintList(list3);
// ListNode *head = MergeList(list,list3);
// PrintList(head);
return 0;
}