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 90d3c12 commit a651ff5Copy full SHA for a651ff5
1 file changed
merge-two-sorted-lists/grapefruit13.ts
@@ -0,0 +1,33 @@
1
+class ListNode {
2
+ val: number;
3
+ next: ListNode | null;
4
+ constructor(val?: number, next?: ListNode | null) {
5
+ this.val = val === undefined ? 0 : val;
6
+ this.next = next === undefined ? null : next;
7
+ }
8
+}
9
+
10
+/**
11
+ * Time Complexity: O(n + m)
12
+ * Space Complexity: O(1)
13
+ */
14
+function mergeTwoLists(
15
+ list1: ListNode | null,
16
+ list2: ListNode | null
17
+): ListNode | null {
18
+ const dummy = new ListNode(0);
19
+ let current = dummy;
20
21
+ while (list1 && list2) {
22
+ if (list1.val < list2.val) {
23
+ current.next = list1;
24
+ list1 = list1.next;
25
+ } else {
26
+ current.next = list2;
27
+ list2 = list2.next;
28
29
+ current = current.next;
30
31
+ current.next = list1 || list2;
32
+ return dummy.next;
33
0 commit comments