Question:
合并两个按顺序排列的链接
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
Code:
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ l2_head = l2 # 用l2_head保存l2的头部 l1_head = l1 # 用l1_head保存l1的头部 while l2_head != None and l1.next != None: # 如果此时l2头部的数在l1和l1.next之间则插入到两者之间 if l1.val <= l2_head.val and l1.next.val >= l2_head.val: l2 = l2.next l1_next = l1.next l1.next = l2_head l1 = l1.next # l1对比过之后也要往后挪一个 l2_head.next = l1_next l2_head = l2 continue elif l1.val > l2_head.val: #如果此时l2_head比l1的小,则把l2_head插到l1前面去,并把l1_head更新 l2 = l2.next l2_head.next = l1 l1_head = l2_head l2_head = l2 # l2要记得往后移一个 continue elif l1.next.val < l2_head.val: l1 = l1.next continue if l2_head == None: return l1_head if l1.next == None: l1.next = l2_head return l1_head # l1=[1,2,4] # l2=[1,3,4] l1 = ListNode(1) #节点2 做头 l1_2 = ListNode(2) l1.next = l1_2 #节点4,放在2的后面 l1_2.next = ListNode(4) #节点3,放在4的后面 l2 = ListNode(1) l2_2 = ListNode(3) l2.next = l2_2 l2_2.next = ListNode(4) s = Solution() l3 = s.mergeTwoLists(l1,l2) print("Test case 1:") while l3 != None: print(l3.val) l3 = l3.next # l1=[1,2,4] # l2=[1,3] l1 = ListNode(1) #节点2 做头 l1_2 = ListNode(2) l1.next = l1_2 #节点4,放在2的后面 l1_2.next = ListNode(4) #节点3,放在4的后面 l2 = ListNode(1) l2_2 = ListNode(3) l2.next = l2_2 s = Solution() l3 = s.mergeTwoLists(l1,l2) print("Test case 2:") while l3 != None: print(l3.val) l3 = l3.next # l1=[1] # l2=[1,3,4] l1 = ListNode(1) #节点2 做头 l2 = ListNode(1) l2_2 = ListNode(3) l2.next = l2_2 l2_2.next = ListNode(4) #节点3,放在4的后面 s = Solution() l3 = s.mergeTwoLists(l1,l2) print("Test case 3:") while l3 != None: print(l3.val) l3 = l3.next # l1=[2,2] # l2=[1,3,4] l1 = ListNode(2) #节点2 做头 l1_2 = ListNode(2) l1.next = l1_2 #节点4,放在2的后面 l2 = ListNode(1) l2_2 = ListNode(3) l2.next = l2_2 l2_2.next = ListNode(4) #节点3,放在4的后面 s = Solution() l3 = s.mergeTwoLists(l1,l2) print("Test case 4:") while l3 != None: print(l3.val) l3 = l3.next # l1=[4,6] # l2=[2] l1 = ListNode(4) #节点2 做头 l1_2 = ListNode(6) l1.next = l1_2 #节点4,放在2的后面 l2 = ListNode(2) s = Solution() l3 = s.mergeTwoLists(l1,l2) print("Test case 5:") while l3 != None: print(l3.val) l3 = l3.next
Result:
Test case 1:
1
1
2
3
4
4
Test case 2:
1
1
2
3
4
Test case 3:
1
1
3
4
Test case 4:
1
2
2
3
4
Test case 5:
2
4
6
Summary:
排序的宗旨是比较几种情况
1.拿出l2的头,和l1的头部做大小对比,决定往哪里插入。并挪动l2和l1的头部,还有最后返回的l1的头部。
2.如果l2到头了,直接返回l1最前面的node
3.如果l1到头了。那么把l1的下一个指向l2的头部,并返回l1最前面那个node