-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path02-线性结构1 两个有序链表序列的合并.c
40 lines (39 loc) · 978 Bytes
/
02-线性结构1 两个有序链表序列的合并.c
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
List Merge( List L1, List L2 )
{
List L, rear, temp;
L = (List)malloc(sizeof(struct Node)); L->Next = NULL;
rear = L;
while ( L1->Next && L2->Next ) {
if (L1->Next->Data < L2->Next->Data) {
temp = L1->Next;
L1->Next = temp->Next;
rear->Next = temp;
rear = temp;
}
else if (L1->Next->Data > L2->Next->Data) {
temp = L2->Next;
L2->Next = temp->Next;
rear->Next = temp;
rear = temp;
}
else {
temp = L1->Next;
L1->Next = temp->Next;
rear->Next = temp;
rear = temp;
temp = L2->Next;
L2->Next = temp->Next;
rear->Next = temp;
rear = temp;
}
}
if (L1->Next) {
rear->Next = L1->Next;
L1->Next = NULL;
}
if (L2->Next) {
rear->Next = L2->Next;
L2->Next = NULL;
}
return L;
}