1. 程式人生 > 資訊 >微軟正在改進 Win10 電池設定和使用情況統計資訊(附手動開啟方法)

微軟正在改進 Win10 電池設定和使用情況統計資訊(附手動開啟方法)

技術標籤:leetcodeleetcode

https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/
輸入兩個遞增排序的連結串列,合併這兩個連結串列並使新連結串列中的節點仍然是遞增排序的。

思路:歸併排序中的歸併過程
雙指標,指標1(index1)指向連結串列1的頭節點,指標2(index2)指向連結串列2的頭節點

  1. 指標1大於指標2,保留連結串列2當前節點,指標2後移
  2. 其餘情況,保留連結串列1當前節點,指標1後移
  3. 重複1,2步驟,直到兩個連結串列都遍歷完畢
    時間複雜度m+n,空間複雜度m+n
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        head = ListNode()
        tmp = head
        index1 = l1
        index2 = l2
        while index1 is not None and index2 is not None:
            if index1.val > index2.val:
                tmp.next = index2
                index2 = index2.next
            else:
                tmp.next = index1
                index1 = index1.next
            tmp = tmp.next
        while index1 is not None:
            tmp.next = index1
            tmp = tmp.next
            index1 = index1.next
        while index2 is not None:
            tmp.next = index2
            tmp = tmp.next
            index2 = index2.next
        return head.next

在這裡插入圖片描述