Ejemplo n.º 1
0
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head

        dummy = ListNode(0)
        dummy.next = head
        prev = dummy
        count = 0
        node = prev.next
        while node:
            count += 1
            if count % 2 == 0:
                prev = self.swap(prev, node)
                node = prev.next
            else:
                node = node.next

        show_linkedlist(dummy.next)
    #     start = dummy
    #     end = head
    #     count = 0
    #     while end:
    #         count += 1
    #         if count % k == 0:
    #             start = self.reverseList(start, end)
    #             end = start.next
    #         else:
    #             end = end.next
    #     return dummy.next
    #
    # def reverseList(self, start, end):
    #     prev = start
    #     tail = end.next
    #     head, cur = start.next, start.next
    #     next = cur.next
    #     while cur is not end:
    #         head.next = next.next
    #         next.next = cur
    #         cur = next
    #         next = head.next
    #     prev.next = cur
    #     head.next = tail
    #     return head


l = make_linkedlist([1,2,3,4,5,6])
l = Solution().reverseKGroup(l, 4)
show_linkedlist(l)
Ejemplo n.º 3
0
    '''
    def removeZeroSumSublists(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return None
        dummy = ListNode(0)
        dummy.next = head
        diff = {}
        sum = 0
        node = dummy
        while node:
            sum += node.val
            diff[sum] = node
            node = node.next

        sum = 0
        node = dummy
        while node:
            sum += node.val
            node.next = diff[sum].next
            node = node.next

        return dummy.next


l = make_linkedlist([1, 2, 3, -3, -2])
show_linkedlist(Solution().removeZeroSumSublists(l))
        dummy = ListNode(0)
        dummy.next = head
        node = dummy
        count = 0
        while node and count != m - 1:
            node = node.next
            count += 1
        print(node.val)
        self.reverse(node, n - m)
        return dummy.next

    def reverse(self, prev, size):

        head = prev.next
        cur = head
        next = cur.next
        count = 0
        while cur and count != size:
            head.next = next.next
            next.next = cur
            cur = next
            next = head.next
            count += 1
        prev.next = cur

l = make_linkedlist([1,2])
show_linkedlist(Solution().reverseBetween(l, 1,2))


        dummy = ListNode(0)
        head = dummy
        while l1 or l2:
            if l1:
                n1 = l1.val
            else:
                n1 = 0
            if l2:
                n2 = l2.val
            else:
                n2 = 0

            res = (n1 + n2 + extra) % 10
            extra = (n1 + n2 + extra) // 10
            dummy.next = ListNode(res)
            if l1:
                l1 = l1.next
            if l2:
                l2 = l2.next
            dummy = dummy.next

        if extra != 0:
            dummy.next = ListNode(extra)
        return head.next


l1 = make_linkedlist([9, 9])
l2 = make_linkedlist([1])
l3 = Solution().addTwoNumbers(l1, l2)
show_linkedlist(l3)
Ejemplo n.º 6
0
    #     if head is None or head.next is None:
    #         return head
    #
    #     node = head
    #     total = {}
    #     while node:
    #         if node.val not in total:
    #             total[node.val] = 1
    #         else:
    #             total[node.val] += 1
    #         node = node.next
    #
    #     dummy = ListNode(0)
    #     dummy.next = head
    #     prev, cur = dummy, head
    #
    #     while cur:
    #         if total[cur.val] > 1:
    #             prev.next = cur.next
    #             cur = prev.next
    #         else:
    #             prev = prev.next
    #             cur = cur.next
    #
    #     return dummy.next

l = make_linkedlist([1,2,2,3,3,4,4,5,5,5,6])
show_linkedlist(Solution().deleteDuplicates(l))


Ejemplo n.º 7
0
class Solution(object):
    '''
    快慢指针 + 虚拟头结点,确保头结点可以删除第一个头结点
    '''
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        if head is None:
            return None
        if head.next is None and n == 1:
            return None
        count = 0
        dummy = ListNode(0)
        dummy.next = head
        node, cur = dummy, dummy
        while node:
            if count > n:
                cur = cur.next
            node = node.next
            count += 1

        cur.next = cur.next.next
        return dummy.next


l = make_linkedlist([1])
show_linkedlist(Solution().removeNthFromEnd(l, 1))
Ejemplo n.º 8
0
    #         return None
    #     if head.next is None or k == 0:
    #         return head
    #     node = head
    #     size = 0
    #     while node:
    #         node = node.next
    #         size += 1
    #     times = k % size
    #
    #     dummy = ListNode(0)
    #     dummy.next = head
    #     for i in range(times):
    #         dummy = self.rotateOnce(dummy)
    #
    #     return dummy.next
    #
    # def rotateOnce(self, head):
    #     node = head
    #     while node.next.next:
    #         node = node.next
    #     tail = node.next
    #     node.next = tail.next
    #     tail.next = head.next
    #     head.next = tail
    #     return head


l = make_linkedlist([1, 2, 3, 4, 5])
show_linkedlist(Solution().rotateRight(l, 2))
    '''
    分治法自顶向下递归分裂链表,排序合并
    时间复杂度O(nlogn)空间复杂度O(N)
    '''
    # def sortList(self, head):
    #     """
    #     :type head: ListNode
    #     :rtype: ListNode
    #     """
    #     return self.divide(head)
    #
    # def divide(self, head):
    #     if head is None or head.next is None:
    #         return head
    #
    #     fast, slow = head, head
    #     while fast.next and fast.next.next:
    #         slow = slow.next
    #         fast = fast.next.next
    #     head2 = slow.next
    #     slow.next = None
    #     h1 = self.divide(head)
    #     h2 = self.divide(head2)
    #     return self.merge(h1, h2)
    #


l = make_linkedlist([4, 2, 1, 3])
show_linkedlist(Solution().sortList(l))
        while l1 or l2:
            if l1:
                stack1.append(l1.val)
                l1 = l1.next
            if l2:
                stack2.append(l2.val)
                l2 = l2.next
        dummy = ListNode(0)

        extra = 0
        while stack1 or stack2:
            n1 = stack1.pop() if stack1 else 0
            n2 = stack2.pop() if stack2 else 0

            sum = n1 + n2 + extra
            node = ListNode(sum % 10)
            extra = sum // 10
            node.next = dummy.next
            dummy.next = node

        if extra != 0:
            node = ListNode(extra)
            node.next = dummy.next
            dummy.next = node
        return dummy.next


l1 = make_linkedlist([9,9,9,9,9])
l2 = make_linkedlist([5,6,4])
show_linkedlist(Solution().addTwoNumbers(l1, l2))
class Solution(object):
    '''
    拿到一个节点将节点拼入新的链表
    时间复杂度O(N)空间复杂度O(1)
    '''
    def insertionSortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head
        dummy = ListNode(-0)
        cur = dummy
        node = head
        while node:
            if cur.next is None or cur.next.val > node.val:
                prev = node.next
                node.next = cur.next
                cur.next = node
                cur = dummy
                node = prev
            else:
                cur = cur.next

        return dummy.next


l = make_linkedlist([-1, 5, 3, 4, 0])
show_linkedlist(Solution().insertionSortList(l))
Ejemplo n.º 12
0
    '''
    def plusOne(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return None

        dummy = ListNode(0)
        dummy.next = head

        nine = dummy
        while head:
            if head.val != 9:
                nine = head
            head = head.next

        node = nine.next
        while node:
            node.val = 0
            node = node.next

        nine.val += 1

        return dummy if dummy.val == 1 else dummy.next


l = make_linkedlist([1, 2, 3])
show_linkedlist(Solution().plusOne(l))
    '''
    def partition(self, head, x):
        """
        :type head: ListNode
        :type x: int
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head

        large = ListNode(0)
        l_node = large
        small = ListNode(0)
        s_node = small
        node = head
        while node:
            if node.val < x:
                s_node.next = node
                s_node = s_node.next
            else:
                l_node.next = node
                l_node = l_node.next
            node = node.next
        l_node.next = None
        s_node.next = large.next
        return small.next


l = make_linkedlist([1, 4, 3, 2, 5, 2])
show_linkedlist(Solution().partition(l, 3))
    偶数指针指向每个新拼接上的偶数
    节点指针遍历链表中的节点
    时间复杂度O(N)空间复杂度O(1)
    '''
    def oddEvenList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head

        even_head = head.next
        odd = head
        even = even_head
        node = even.next
        while node:
            next_odd = node.next.next if node.next else None
            even.next = node.next
            odd.next = node
            node.next = even_head
            odd = odd.next
            even = even.next
            node = next_odd

        return head


l = make_linkedlist([1,2,3,4,5])
show_linkedlist(Solution().oddEvenList(l))