Skip to content

Commit

Permalink
refactor: rename variables for solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrig committed Jul 23, 2023
1 parent 2d289e3 commit 2147683
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions typescript/src/mergeTwoSortedLists/mergeTwoSortedLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ export class ListNode {
}

export function mergeTwoLists(
l1: ListNode | null,
l2: ListNode | null
list1: ListNode | null,
list2: ListNode | null
): ListNode | null {
// Create a new linked list that will contain the merged nodes
const dummyHead = new ListNode(-1);
let current = dummyHead;

// We continue to merge nodes until there is at least one node in each list
while (l1 !== null && l2 !== null) {
if (l1.val <= l2.val) {
current.next = l1;
l1 = l1.next;
while (list1 !== null && list2 !== null) {
if (list1.val <= list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = l2;
l2 = l2.next;
current.next = list2;
list2 = list2.next;
}
current = current.next;
}

// If there are nodes in the first list, add them to the end of the new list
if (l1 !== null) {
current.next = l1;
if (list1 !== null) {
current.next = list1;
}

// If there are nodes in the second list, add them to the end of the new list
if (l2 !== null) {
current.next = l2;
if (list2 !== null) {
current.next = list2;
}

return dummyHead.next;
Expand Down

0 comments on commit 2147683

Please sign in to comment.