Ejemplo n.º 1
0
    def detectCycle2(self, head):
        """
        检测链表是否有环,O(1)空间复杂度
        :type head: ListNode
        :rtype: bool
        """
        if not head:
            return None
        fast = head
        slow = head
        while fast and fast.next and slow:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                break
        if not fast or not fast.next:
            return None
        fast = head
        while fast != slow:
            fast = fast.next
            slow = slow.next
        return fast


if __name__ == '__main__':
    head = ListNode.create_linked_list([1, 2, 3])
    # head.next.next.next = head
    res = Solution().detectCycle2(head)
    print(res is not None)
class Solution(object):
    def deleteDuplicates(self, head):
        """
        删除重复的节点
        :type head: ListNode
        :rtype: ListNode
        """
        temp = head
        pre = None
        while temp:
            if temp.next and temp.val == temp.next.val:
                t = temp.next
                while t and temp.val == t.val:
                    t = t.next
                temp = t
                if not pre:
                    head = temp
                else:
                    pre.next = temp
            else:
                pre = temp
                temp = temp.next
        return head


if __name__ == '__main__':
    list = ListNode.create_linked_list([1, 2, 3, 3, 4, 4, 5])
    result = Solution().deleteDuplicates(list)
    print(result)
Ejemplo n.º 3
0
        return head.next

    def mergeKLists3(self, lists):
        """
        合并多个排序好的列表
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        node_list = []
        for node in lists:
            while node:
                node_list.append(node)
                node = node.next
        if not node_list:
            return []
        node_list = sorted(node_list, key=lambda x: x.val)
        head = cur = ListNode(0)
        for node in node_list:
            cur.next = node
            cur = node
        return head.next


if __name__ == '__main__':
    input = [
        ListNode.create_linked_list([0]),
        ListNode.create_linked_list([1]),
    ]
    result = Solution().mergeKLists3(input)
    print(result)
        合并两个有序链表
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        t1 = l1
        t2 = l2
        cur = head = ListNode(0)
        while t1 or t2:
            if not t1:
                cur.next = ListNode(t2.val)
                t2 = t2.next
            elif not t2:
                cur.next = ListNode(t1.val)
                t1 = t1.next
            elif t1.val < t2.val:
                cur.next = ListNode(t1.val)
                t1 = t1.next
            else:
                cur.next = ListNode(t2.val)
                t2 = t2.next
            cur = cur.next
        return head.next


if __name__ == '__main__':
    list1 = ListNode.create_linked_list([2, 3])
    list2 = ListNode.create_linked_list([1, 2])
    result = Solution().mergeTwoLists(list1, list2)
    print(result)
        """
        将两个链表形式的数字相加
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        t1 = l1
        t2 = l2
        cur = head = ListNode(0)
        d = 0
        while t1 or t2:
            cur.next = ListNode(0)
            cur = cur.next
            val1 = t1.val if t1 else 0
            val2 = t2.val if t2 else 0
            d, cur.val = divmod(val1 + val2 + d, 10)
            if t1:
                t1 = t1.next
            if t2:
                t2 = t2.next
        if d:
            cur.next = ListNode(d)
        return head.next


if __name__ == '__main__':
    l1 = ListNode.create_linked_list([1, 1])
    l2 = ListNode.create_linked_list([9, 9])
    result = Solution().addTwoNumbers(l1, l2)
    print(result)