File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode() : val(0), next(nullptr) {}
7
+ * ListNode(int x) : val(x), next(nullptr) {}
8
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
9
+ * };
10
+ */
11
+ class Solution {
12
+ public:
13
+ ListNode* mergeTwoLists (ListNode* list1, ListNode* list2) {
14
+ ListNode root = ListNode ();
15
+ ListNode *current = &root;
16
+
17
+ ListNode *smaller = list1;
18
+ ListNode *bigger = list2;
19
+
20
+ while (smaller && bigger) {
21
+ if (smaller->val > bigger->val ) {
22
+ swap (smaller, bigger);
23
+ }
24
+
25
+ current->next = smaller;
26
+ current = current->next ;
27
+ smaller = smaller->next ;
28
+ }
29
+
30
+ if (smaller) {
31
+ current->next = smaller;
32
+ }
33
+ if (bigger) {
34
+ current->next = bigger;
35
+ }
36
+
37
+ return root.next ;
38
+ }
39
+ };
You can’t perform that action at this time.
0 commit comments