We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent dfa0f77 commit 7795f81Copy full SHA for 7795f81
merge-two-sorted-lists/delight010.swift
@@ -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