Linked list
Easy
You are given the heads of two sorted linked lists list1
and list2
.
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Learn More →
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Input: list1 = [], list2 = []
Output: []
Input: list1 = [], list2 = [0]
Output: [0]
var mergeTwoLists = function(l1, l2) {
let dummy = new ListNode();
let currTail = dummy;
while(l1 && l2) {
if(l1.val > l2.val) {
currTail.next = l2;
l2 = l2.next;
} else {
currTail.next = l1;
l1 = l1.next;
}
currTail = currTail.next;
}
currTail.next = l1 || l2;
return dummy.next;
};
var mergeTwoLists = function(list1, list2) {
let head = null;
if (!list1 && list2) return list2;
if (!list2 && list1) return list1;
if (!list1 && !list2) return null;
if (list1.val < list2.val){
head = new ListNode(list1.val)
list1 = list1.next;
} else {
head = new ListNode(list2.val)
list2 = list2.next;
}
let currNode = head;
while(list1 !== null && list2 !== null){
if (list1.val < list2.val){
currNode.next = new ListNode(list1.val)
list1 = list1.next;
} else {
currNode.next = new ListNode(list2.val)
list2 = list2.next;
}
currNode = currNode.next;
}
if (list1 !== null) currNode.next = list1;
if (list2 !== null) currNode.next = list2;
return head;
};
Sorry… but please explain.
/**
* Why it fails the tests?
*/
var mergeTwoLists = function (list1, list2) {
const getHeadNode = () => {
if (!list1) return list2;
if (!list2) return list1;
return list1.value <= list2.value ? list1 : list2;
};
const headNode = getHeadNode();
if (headNode) {
let curListNode = headNode;
let otherListNode = headNode === list1 ? list2 : list1;
while (otherListNode) {
if (!curListNode.next || curListNode.next.value > otherListNode.value) {
const newCurListNextNode = otherListNode;
otherListNode = curListNode.next;
curListNode.next = newCurListNextNode;
};
curListNode = curListNode.next;
}
}
return headNode;
};
var mergeTwoLists = function(list1, list2) {
// time:O(m+n) space:O(m+n) n = list1 length, m = list2 length
if(!list1 || !list2){
return list1 ? list1:list2
}
let mergedList = new ListNode(0,null);
let currentNode = mergedList;
while(list1 && list2){
if(list1.val < list2.val){
currentNode.next = list1
list1 = list1.next
}else{
currentNode.next = list2
list2 = list2.next
}
currentNode = currentNode.next
}
if(!list1){
currentNode.next = list2
}else if(!list2){
currentNode.next = list1
}
return mergedList.next
};
??
/**
* time: O(m+n) - where m is the number of nodes in the list1, n is the number of nodes in the list2
* space: O(m+n) - where m is the number of nodes in the list1, n is the number of nodes in the list2
*/
var mergeTwoLists = function(list1, list2) {
if(!list1) return list2;
if(!list2) return list1;
const dummy = [];
let curr = dummy;
while(list1 && list2){
if(list1.val < list2.val){
curr.next = list1;
list1 = list1.next;
}
else{
curr.next = list2;
list2 = list2.next;
}
curr = curr.next;
}
curr.next = list1 ?? list2;
return dummy.next;
};
//time: O(n) | space: O(n)
var mergeTwoLists = function(list1, list2) {
const head = new ListNode();
currentNode = head;
while( list1 && list2){
if( list1.val >= list2.val){
currentNode.next = list2;
list2 = list2.next;
}else{
currentNode.next = list1;
list1 = list1.next;
}
currentNode = currentNode.next
}
//假設其中一個list先null
if (list1){
currentNode.next = list1
}else{
currentNode.next = list2
}
return head.next
};
var mergeTwoLists = function(l1, l2) {
// Time O(n) Space O(n)
const result = new ListNode(0);
let pointer = result; // Why pointer can reaction to result?
while(l1 && l2){
if(l1.val < l2.val){
pointer.next = l1; // Why not l1.val?
l1 = l1.next;
} else {
pointer.next = l2;
l2 = l2.next
}
pointer = pointer.next;
}
pointer.next = l1 || l2;
return result.next
};
let dummy = new ListNode();
let currTail = dummy;
// some work
return dummy.next;
??
You're given two Linked Lists of potentially unequal length. These Linked Lists potentially merge at a shared intersection node. Write a function that returns the intersection node or returns None / null if there is no intersection.Each LinkedList node has an integer value as well as a next node pointing to the next node in the list or to None null if it's the tail of the list.Note: Your function should return an existing node. It should not modify either Linked List, and it should not create any new Linked Lists.
Feb 2, 2025Write a function that takes in the head of a Singly Linked List and an integer k and removes the kth node from the end of the list.The removal should be done in place, meaning that the original data structure should be mutated (no new structure should be created).Furthermore, the input head of the linked list should remain the head of the linked list after the removal is done, even if the head is the node that's supposed to be removed. In other words, if the head is the node that's supposed to be removed, your function should simply mutate its value and next pointer.Note that your function doesn't need to return anything.You can assume that the input Linked List will always have at least two nodes and, more specifically, at least k nodes.Each LinkedList node has an integer value as well as a next node pointing to the next node in the list or to None / null if it's the tail of the list.
Feb 2, 2025https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/1735550/python-javascript-easy-solution-with-very-clear-explanation/
Aug 27, 2023https://leetcode.com/problems/container-with-most-water/solutions/3493276/c-java-python-javascript-optimized-code-easy-to-understand-100-solution-explained/
Aug 22, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up