Skip to content

Commit

Permalink
[Leetcode - Easy] Merge Two Sorted Lists (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
BangDori authored Nov 18, 2024
1 parent 41d1064 commit 4710b04
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions bangdori/Merge Two Sorted Lists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
var mergeTwoLists = function (list1, list2) {
if (!list1) return list2;
if (!list2) return list1;

let list = new ListNode();
const head = list;

while (list1 && list2) {
if (list1.val < list2.val) {
list.next = list1;
list1 = list1.next;
} else {
list.next = list2;
list2 = list2.next;
}

list = list.next;
}

if (!list1) {
list.next = list2;
}
if (!list2) {
list.next = list1;
}

return head.next;
};

0 comments on commit 4710b04

Please sign in to comment.