链表类——合并链表
序号 | 题目 | 难度 | 代码 |
---|---|---|---|
2 | Add Two Numbers | medium | python、java、c++ |
21 | Merge Two Sorted Lists | Easy | python、java、c++ |
23 | Merge k Sorted Lists | Hard | python、java、c++ |
445 | Add Two Numbers II | Medium | python、java、c++ |
2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1+v2+carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
21. Merge Two Sorted Lists
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 and l2:
if l1.val >l2.val:
l1,l2 = l2,l1
l1.next = self.mergeTwoLists(l1.next,l2)
return l1 or l2
23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input: [ 1->4->5, 1->3->4, 2->6 ]
Output: 1->1->2->3->4->4->5->6
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
pre = cur = ListNode(0)
heap = []
for i in range(len(lists)):
if lists[i]:
heapq.heappush(heap, (lists[i].val, i, lists[i]))
while heap:
node = heapq.heappop(heap)
idx = node[1]
cur.next = node[2]
cur = cur.next
if cur.next:
heapq.heappush(heap, (cur.next.val, idx, cur.next))
return pre.next
445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
l1 = self.reverseLL(l1)
l2 = self.reverseLL(l2)
car = 0
sen = ListNode(0)
cur = sen
while l1 or l2 or car:
if l1 and l2:
tot = l1.val + l2.val + car
l1, l2 = l1.next, l2.next
elif l1:
tot = l1.val + car
l1 = l1.next
elif l2:
tot = l2.val + car
l2 = l2.next
else:
tot = car
cur.next, car = ListNode(tot%10), tot//10
cur = cur.next
return self.reverseLL(sen.next)
def reverseLL(self, head):
prev, cur = None, head
while cur:
cur.next, prev, cur = prev, cur, cur.next
return prev