The number of nodes in both lists is in the range [0, 50].
-100 <= Node.val <= 100
Both list1 and list2 are sorted in non-decreasing order.
Solutions
Solution 1: Recursion
First, we judge whether the linked lists \(l_1\) and \(l_2\) are empty. If one of them is empty, we return the other linked list. Otherwise, we compare the head nodes of \(l_1\) and \(l_2\):
If the value of the head node of \(l_1\) is less than or equal to the value of the head node of \(l_2\), we recursively call the function \(mergeTwoLists(l_1.next, l_2)\), and connect the head node of \(l_1\) with the returned linked list head node, and return the head node of \(l_1\).
Otherwise, we recursively call the function \(mergeTwoLists(l_1, l_2.next)\), and connect the head node of \(l_2\) with the returned linked list head node, and return the head node of \(l_2\).
The time complexity is \(O(m + n)\), and the space complexity is \(O(m + n)\). Here, \(m\) and \(n\) are the lengths of the two linked lists respectively.
1 2 3 4 5 6 7 8 91011121314151617
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclassSolution:defmergeTwoLists(self,list1:Optional[ListNode],list2:Optional[ListNode])->Optional[ListNode]:iflist1isNoneorlist2isNone:returnlist1orlist2iflist1.val<=list2.val:list1.next=self.mergeTwoLists(list1.next,list2)returnlist1else:list2.next=self.mergeTwoLists(list1,list2.next)returnlist2
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */funcmergeTwoLists(list1*ListNode,list2*ListNode)*ListNode{iflist1==nil{returnlist2}iflist2==nil{returnlist1}iflist1.Val<=list2.Val{list1.Next=mergeTwoLists(list1.Next,list2)returnlist1}else{list2.Next=mergeTwoLists(list1,list2.Next)returnlist2}}
/** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val = 0, $next = null) { * $this->val = $val; * $this->next = $next; * } * } */class Solution { /** * @param ListNode $list1 * @param ListNode $list2 * @return ListNode */ function mergeTwoLists($list1, $list2) { if (is_null($list1)) { return $list2; } if (is_null($list2)) { return $list1; } if ($list1->val <= $list2->val) { $list1->next = $this->mergeTwoLists($list1->next, $list2); return $list1; } else { $list2->next = $this->mergeTwoLists($list1, $list2->next); return $list2; } }}
Solution 2: Iteration
We can also use iteration to implement the merging of two sorted linked lists.
First, we define a dummy head node \(dummy\), then loop through the two linked lists, compare the head nodes of the two linked lists, add the smaller node to the end of \(dummy\), until one of the linked lists is empty, then add the remaining part of the other linked list to the end of \(dummy\).
Finally, return \(dummy.next\).
The time complexity is \(O(m + n)\), where \(m\) and \(n\) are the lengths of the two linked lists respectively. Ignoring the space consumption of the answer linked list, the space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9101112131415161718192021
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclassSolution:defmergeTwoLists(self,list1:Optional[ListNode],list2:Optional[ListNode])->Optional[ListNode]:dummy=ListNode()curr=dummywhilelist1andlist2:iflist1.val<=list2.val:curr.next=list1list1=list1.nextelse:curr.next=list2list2=list2.nextcurr=curr.nextcurr.next=list1orlist2returndummy.next
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */funcmergeTwoLists(list1*ListNode,list2*ListNode)*ListNode{dummy:=&ListNode{}curr:=dummyforlist1!=nil&&list2!=nil{iflist1.Val<=list2.Val{curr.Next=list1list1=list1.Next}else{curr.Next=list2list2=list2.Next}curr=curr.Next}iflist1!=nil{curr.Next=list1}else{curr.Next=list2}returndummy.Next}