File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * type ListNode struct {
4
+ * Val int
5
+ * Next *ListNode
6
+ * }
7
+ */
8
+ func mergeTwoLists (l1 * ListNode , l2 * ListNode ) * ListNode {
9
+ if l1 == nil {
10
+ return l2
11
+ }
12
+
13
+ if l2 == nil {
14
+ return l1
15
+ }
16
+
17
+ ptr1 , ptr2 := l1 , l2
18
+
19
+ result := new (ListNode )
20
+
21
+ temp := result
22
+
23
+ for ptr1 != nil && ptr2 != nil {
24
+ if ptr1 .Val < ptr2 .Val {
25
+ temp .Next = ptr1
26
+ temp = temp .Next
27
+ ptr1 = ptr1 .Next
28
+ } else {
29
+ temp .Next = ptr2
30
+ temp = temp .Next
31
+ ptr2 = ptr2 .Next
32
+ }
33
+ }
34
+
35
+ for ptr1 != nil {
36
+ temp .Next = ptr1
37
+ temp = temp .Next
38
+ ptr1 = ptr1 .Next
39
+ }
40
+
41
+ for ptr2 != nil {
42
+ temp .Next = ptr2
43
+ temp = temp .Next
44
+ ptr2 = ptr2 .Next
45
+ }
46
+
47
+ result = result .Next
48
+ return result
49
+ }
You can’t perform that action at this time.
0 commit comments