Yup… oddly like merging into traffic. 😆 😂 😝
The Prompt:Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.
Illustration: Merging two sorted linked lists
class List {
constructor(val = null, next = null) {
this.val = val <= number value of current node
this.next = next <= rest of values AFTER current node !
}
}
let mergeTwoLists = function(l1, l2) {let result = new List(-1)
let head = result
}
while(l1 !== null && l2 !== null) {

... Both lists shouldn't be null ...

};
Note: use '<=' , it deals with any ties !!!if(l1.val <= l2.val) {  ...  } 

result = result.next <= so we don't overwrite our added node !!!
if(l1.val <= l2.val) {.   <= what's the lower value? Who wins ???            result.next = l1.next   <= if winner, add whole list 
l1 = l1.next <= update list without current value
} else {
result.next = l2.next <= if winner, same deal
l2 = l2.next <= update the same way
...
if(l1 !== null) {          <= If first list IS NOT null
result.next = l1 <= add rest of list to result

} else {
result.next = l2 <= add rest of list to result
}

return head.next <= We don't want the first value, just everything after it
};
const mergeTwoLists = function(l1, l2) {
let result = new List(-1)
let head = result

while(l1 !== null && l2 !== null) {

if(l1.val <= l2.val) {
result.next = l1
l1 = l1.next
} else {
result.next = l2
l2 = l2.next
}
result = result.next
};

if(l1 !== null) {
result.next = l1

} else {
result.next = l2
}

return head.next

};
class List {
constructor(val = null, next = null) {
this.val = val
this.next = next
}
}

--

--

--

Career Changer, Software Engineer & Web Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

This Changes The Way You “See” Quantum Computing

Converting Figma designs to UWP XAML

8 Things All Developers Have Dealt With Before

Software Development Ain’t a 9 am to 5 pm Job

Kafka Installation step by step in Ubuntu 18.04

Using Tekton Conditions To Tackle Monorepos

Ubuntu 20.04 LTS Released! Focal Fossa is Out — Tec Robust

1Hive Monthly Recap February 2022

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Lesser

Sam Lesser

Career Changer, Software Engineer & Web Developer

More from Medium

How to perform range addition in O(n) time and O(1) space complexity?

What is a Deque?

[Leetcode 525] Contiguous Array

Longest Substring Without Repeating Characters📸