Skip to content

Commit 7795f81

Browse files
committed
solve problem
1 parent dfa0f77 commit 7795f81

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class ListNode {
2+
public var val: Int
3+
public var next: ListNode?
4+
public init() { self.val = 0; self.next = nil; }
5+
public init(_ val: Int) { self.val = val; self.next = nil; }
6+
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
7+
}
8+
9+
class Solution {
10+
func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
11+
guard let list1 = list1 else {
12+
return list2
13+
}
14+
guard let list2 = list2 else {
15+
return list1
16+
}
17+
18+
if list1.val < list2.val {
19+
list1.next = mergeTwoLists(list1.next, list2)
20+
} else {
21+
list2.next = mergeTwoLists(list2.next, list1)
22+
}
23+
return list1.val < list2.val ? list1 : list2
24+
}
25+
}
26+

0 commit comments

Comments
 (0)