Knowee
Questions
Features
Study Tools

You are given the heads of two sorted linked lists list1 and list2.Merge the two lists into 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. Example 1:Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4]Example 2:Input: list1 = [], list2 = []Output: []Example 3:Input: list1 = [], list2 = [0]Output: [0] Constraints:The number of nodes in both lists is in the range [0, 50].-100 <= Node.val <= 100Both list1 and list2 are sorted in non-decreasing order.

Question

You are given the heads of two sorted linked lists list1 and list2.Merge the two lists into 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. Example 1:Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4]Example 2:Input: list1 = [], list2 = []Output: []Example 3:Input: list1 = [], list2 = [0]Output: [0] Constraints:The number of nodes in both lists is in the range [0, 50].-100 <= Node.val <= 100Both list1 and list2 are sorted in non-decreasing order.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, we can use a simple iterative approach where we compare the nodes of the two lists and append the smaller one to our result list. Here is a step-by-step Python solution:

  1. First, we create a dummy node and a current node pointing to the dummy. The dummy node is used to keep track of the head of our result list.
dummy = ListNode(0)
current = dummy
  1. Then, we start a while loop that continues until we have traversed through both lists.
while list1 and list2:
  1. Inside the loop, we compare the values of the nodes of the two lists. If the value of the node in list1 is smaller, we append it to our result list and move to the next node in list1. Otherwise, we append the node from list2 and move to the next node in list2.
if list1.val < list2.val:
    current.next = list1
    list1 = list1.next
else:
    current.next = list2
    list2 = list2.next
current = current.next
  1. After the loop, at least one of the lists has been completely traversed. If there are still nodes left in either of the lists, we append them to our result list.
if list1:
    current.next = list1
elif list2:
    current.next = list2
  1. Finally, we return the next of our dummy node, which is the head of our merged list.
return dummy.next

This solution works in O(n) time, where n is the total number of nodes in both lists. It also uses O(1) extra space, as we are reusing the nodes from the input lists.

This problem has been solved

Similar Questions

class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: cur = dummy = ListNode() while list1 and list2: if list1.val < list2.val: cur.next = list1 list1, cur = list1.next, list1 else: cur.next = list2 list2, cur = list2.next, list2 if list1 or list2: cur.next = list1 if list1 else list2 return dummy.next

Given the head pointers of two linked lists, add them and return the new linked list. Each linked list represents an integer number (each node is a digit).

You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.Merge all the linked-lists into one sorted linked-list and return it. Example 1:Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[ 1->4->5, 1->3->4, 2->6]merging them into one sorted list:1->1->2->3->4->4->5->6Example 2:Input: lists = []Output: []Example 3:Input: lists = [[]]Output: [] Constraints:k == lists.length0 <= k <= 1040 <= lists[i].length <= 500-104 <= lists[i][j] <= 104lists[i] is sorted in ascending order.The sum of lists[i].length will not exceed 104.

You are given two linked lists: list1 and list2 of sizes n and m respectively.Remove list1's nodes from the ath node to the bth node, and put list2 in their place.The blue edges and nodes in the following figure indicate the result:

# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: result=ListNode()… print(minheap) return result

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.