next return list class Solution: def mergeTwoLists(self, head1, head2): if not head1: # 如果head1为空,直接返回head2 return head2 if not head2: # 如果head2为空,直接返回head1 return head1 pre = ListNode(0) # 使用哑结点简化操作 head = pre while head1 and head2: if head1.val ...
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 解题思路: 1)定义l,每次l指向两个链表中小的那个节点,定义head为头指针,定义ptr,将l依次连成链表 2)两个链表均不为空,将其小的值赋给l 3)将其中节...
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: # 使用一个哨兵结点 head_pre ...
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 和88. Merge Sorted Array类似,数据结构不一样,这里是合并链表。 由于是链...
LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 ...
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. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: ...
You are given the heads of two sorted linked listslist1andlist2. Merge the two lists into onesortedlist. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
1. 题目描述 Merge two sorted linked lists and return it as a new sorted list. The new list ...
Merge two sorted linked lists and return it as asortedlist. The list should be made by splicing together the nodes of the first two lists. Example 1: img Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4]
在当前两个链表的节点都是非空的情况下比较大小,较小的添入结果链表中并且获得较小节点的下一个节点。这样比较直至至少遍历完一个链表。再将剩下的链表添至...