-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_C_linklist.cpp
More file actions
89 lines (73 loc) · 1.86 KB
/
Copy pathleetcode_C_linklist.cpp
File metadata and controls
89 lines (73 loc) · 1.86 KB
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
#include <bits/stdc++.h>
using namespace std;
class ListNode
{
public:
int val;
ListNode *next = NULL;
ListNode(int val)
{
this->val = val;
}
};
class Solution
{
public:
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2)
{
ListNode* dummy = new ListNode(-101);
ListNode* tail = dummy;
while (list1 != nullptr && list2 != nullptr) {
if (list1->val <= list2->val) {
tail->next = list1;
list1 = list1->next;
} else {
tail->next = list2;
list2 = list2->next;
}
tail = tail->next;
}
if(list1 != nullptr){
tail->next = list1;
}else if (list2 != nullptr){
tail->next = list2;
}
return dummy->next;
}
ListNode* swapPairs(ListNode* head){
if(head==nullptr || head->next==nullptr) return head;
ListNode* dummy = new ListNode(-101);
dummy->next = head;
ListNode* prev = dummy;
ListNode* current = head;
while (current->next != nullptr)
{
ListNode* next = current->next->next;
ListNode* temp = current->next;
prev->next = current->next;
current->next = next;
prev->next->next = current;
prev = prev->next;
current = next;
}
return dummy->next;
}
};
int main()
{
ListNode *list1 = new ListNode(4);
list1->next = new ListNode(5);
list1->next = new ListNode(6);
list1->next = new ListNode(7);
ListNode *list2 = new ListNode(1);
list2->next = new ListNode(2);
list2->next = new ListNode(4);
Solution s1;
ListNode *head = s1.mergeTwoLists(list1, list2);
while (head != NULL)
{
cout << head->val << endl;
head = head->next;
}
return 0;
}