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 955cce8 commit 0160870Copy full SHA for 0160870
1 file changed
merge-two-sorted-lists/junzero741.ts
@@ -0,0 +1,30 @@
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
+function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
12
13
+ const dummy = new ListNode();
14
+ let tail = dummy;
15
16
+ while (list1 && list2) {
17
+ if (list1.val <= list2.val) {
18
+ tail.next = list1;
19
+ list1 = list1.next;
20
+ } else {
21
+ tail.next = list2;
22
+ list2 = list2.next;
23
24
+ tail = tail.next;
25
26
27
+ tail.next = list1 || list2;
28
29
+ return dummy.next;
30
+};
0 commit comments