Esempio n. 1
0
    def reorderList(self, head: ListNode) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
        if not head:
            return head
        deque = []
        pointer = head
        while pointer:
            deque.append(pointer)
            pointer = pointer.next

        p = deque.pop(0)
        while deque:
            p.next = deque.pop(-1)
            p = p.next

            if deque:
                p.next = deque.pop(0)
                p = p.next

        p.next = None
        prettyPrintLinkedList(head)
    def isPalindrome(self, head: ListNode) -> bool:
        if not head or head.next is None:
            return True
        p1 = p2 = head
        while p2.next and p2.next.next:
            p1 = p1.next
            p2 = p2.next.next

        h = p1.next
        t = [p2, p2.next][p2.next is not None]

        def reverse(subhead, tail):
            if subhead == tail:  # 相同则不需要翻转
                return subhead, tail
            current = subhead
            nextnode = current.next
            while nextnode.next:  # 反指并移动指针
                nextnxt = nextnode.next
                nextnode.next = current
                current = nextnode
                nextnode = nextnxt
            nextnode.next = current  # 把最后一步指过来
            return tail, subhead

        h, t = reverse(h, t)
        p1.next = h
        t.next = None
        p2 = head
        prettyPrintLinkedList(head)
        p1 = p1.next
        while p1:
            if p1.val != p2.val:
                return False
            p1 = p1.next
            p2 = p2.next

        return True
            while nextnode.next:  # 反指并移动指针
                nextnxt = nextnode.next
                nextnode.next = current
                current = nextnode
                nextnode = nextnxt
            nextnode.next = current  # 把最后一步指过来
            return tail, subhead

        h, t = reverse(h, t)
        p1.next = h
        t.next = None
        p2 = head
        prettyPrintLinkedList(head)
        p1 = p1.next
        while p1:
            if p1.val != p2.val:
                return False
            p1 = p1.next
            p2 = p2.next

        return True


sol = Solution()
from L61_Rotate_List_linked_list_debug import stringToListNode, prettyPrintLinkedList

head = stringToListNode([1, 2, 3, 6, 2, 1])
head = stringToListNode([1])
prettyPrintLinkedList(head)
print(sol.isPalindrome(head))
Esempio n. 4
0
        """
        if not head:
            return head
        deque = []
        pointer = head
        while pointer:
            deque.append(pointer)
            pointer = pointer.next

        p = deque.pop(0)
        while deque:
            p.next = deque.pop(-1)
            p = p.next

            if deque:
                p.next = deque.pop(0)
                p = p.next

        p.next = None
        prettyPrintLinkedList(head)


from L61_Rotate_List_linked_list_debug import stringToListNode, prettyPrintLinkedList

sol = Solution()

head = stringToListNode([0, 1, 2, 3, 4, 5, 6])
head = stringToListNode([])
prettyPrintLinkedList(head)
prettyPrintLinkedList(sol.reorderList(head))
Esempio n. 5
0
            if stack1 and stack2:
                val1, val2 = stack1.pop(-1).val, stack2.pop(-1).val
            elif stack1:
                val1 = stack1.pop(-1).val
                val2 = 0
            else:
                val1 = 0
                val2 = stack2.pop(-1).val

            val = (val1 + val2 + carry) % 10
            carry = (val1 + val2 + carry) // 10
            ret.append(ListNode(val))
        if carry:
            ret.append(ListNode(1))

        ret.reverse()
        for i, ele in enumerate(ret[:-1]):
            ele.next = ret[i + 1]

        return ret[0]


from L61_Rotate_List_linked_list_debug import stringToListNode, prettyPrintLinkedList

sol = Solution()
head1 = stringToListNode([])
prettyPrintLinkedList(head1)
head2 = stringToListNode([])
prettyPrintLinkedList(head2)
prettyPrintLinkedList(sol.addTwoNumbers(head1, head2))
Esempio n. 6
0
        if not head or not head.next:
            return head  # termination.

        # cut the LinkedList at the mid index.
        slow, fast = head, head.next
        while fast and fast.next:
            fast, slow = fast.next.next, slow.next
        mid, slow.next = slow.next, None  # save and cut.
        # cut recursive.
        left, right = self.sortList(head), self.sortList(mid)

        # merge `left` and `right` linked list and return it.
        h = res = ListNode(0)
        while left and right:
            if left.val < right.val:
                h.next, left = left, left.next
            else:
                h.next, right = right, right.next
            h = h.next
        h.next = left if left else right
        return res.next


from L61_Rotate_List_linked_list_debug import stringToListNode, prettyPrintLinkedList

head = stringToListNode([4, 5, 4, 3, 1, 0, 2])
head = stringToListNode([])
prettyPrintLinkedList(head)
sol = Solution()
prettyPrintLinkedList(sol.sortList(head))
            while t and cnt < k:
                t = t.next
                cnt += 1

            if t:
                nxt = t.next

                h, t = reverse(h, t)

                # connect pre and nxt
                pre.next = h
                t.next = nxt
                # prepare for next round
                pre = t
                h, t = nxt, nxt

        return dummy.next


sol = Solution()
from L61_Rotate_List_linked_list_debug import stringToListNode, prettyPrintLinkedList

# head = stringToListNode([0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11])
# prettyPrintLinkedList(head)
# prettyPrintLinkedList(sol.reverseKGroup(head, 3))
head = stringToListNode([1, 2, 3, 4, 5])
prettyPrintLinkedList(head)
prettyPrintLinkedList(sol.reverseKGroup(head, -1))

# print(sol.reverseKGroup())