Пример #1
0
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if head is None or k == 0:
            return head
        sentinel = ListNode(-1)
        sentinel.next = head

        pre = sentinel
        post = sentinel
        length = 0
        while head is not None:
            length += 1
            head = head.next
        k = k % length

        for i in range(0, k):
            post = post.next

        while post.next is not None:
            post = post.next
            pre = pre.next

        post.next = sentinel.next
        sentinel.next = pre.next
        pre.next = None

        return sentinel.next
Пример #2
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        guard = ListNode(-1)
        ans = guard
        carry_bit = 0

        while l1 or l2:
            if l1 and l2:
                sum = l1.val + l2.val
                l1 = l1.next
                l2 = l2.next
            elif l1:
                sum = l1.val
                l1 = l1.next
            elif l2:
                sum = l2.val
                l2 = l2.next

            sum = sum + carry_bit
            carry_bit = 1 if sum > 9 else 0

            node = ListNode(sum % 10)
            ans.next = node
            ans = node

        if carry_bit == 1:
            ans.next = ListNode(1)

        return guard.next
Пример #3
0
 def reverseListForHeadInsert(self, head: ListNode) -> ListNode:
     guard = ListNode(-1)
     while head is not None:
         temp = head.next
         head.next = guard.next
         guard.next = head
         head = temp
     return guard.next
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        guard = ListNode(-1)
        guard.next = head
        pre, curr = guard, guard
        for i in range(0, n):
            if curr is None:
                return None
            curr = curr.next

        while curr.next is not None:
            pre = pre.next
            curr = curr.next
        pre.next = pre.next.next

        return guard.next
Пример #5
0
    def swapPairs(self, head: ListNode) -> ListNode:
        guard = ListNode(0)
        guard.next = head

        pre = guard
        while pre.next is not None and pre.next.next is not None:
            l = pre.next
            r = pre.next.next
            print(l.val, r.val)

            l.next = r.next
            r.next = l
            pre.next = r

            pre = l

        return guard.next
Пример #6
0
    def recursionFunc(self, node: ListNode) -> ListNode:
        if node.next is None:
            return node

        head = self.recursionFunc(node.next)
        node.next.next = node
        node.next = None
        return head
Пример #7
0
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        length = 1
        tail = head
        while tail.next is not None:
            tail = tail.next
            length += 1
        tail.next = head

        step = length - k % length
        for i in range(0, step - 1):
            head = head.next

        ans = head.next
        head.next = None
        return ans
Пример #8
0
    def conquer(self, ll: ListNode, lr: ListNode):
        guard = ListNode(-1)
        lm = guard

        while ll is not None and lr is not None:
            if ll.val < lr.val:
                lm.next = ll
                ll = ll.next
            else:
                lm.next = lr
                lr = lr.next
            lm = lm.next

        if ll is not None:
            lm.next = ll
        else:
            lm.next = lr

        return guard.next
Пример #9
0
    def merge(self, ll: ListNode, lr: ListNode) -> ListNode:
        guard = ListNode(0)
        lm = guard

        while ll and lr:
            if ll.val < lr.val:
                lm.next = ll
                ll = ll.next
            else:
                lm.next = lr
                lr = lr.next
            lm = lm.next

        if ll:
            lm.next = ll

        if lr:
            lm.next = lr
        return guard.next
Пример #10
0
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        guard = ListNode(-1)
        ans = guard
        l1_ptr, l2_ptr = l1, l2

        while l1_ptr is not None and l2_ptr is not None:
            if l1_ptr.val < l2_ptr.val:
                ans.next = l1_ptr
                l1_ptr = l1_ptr.next
            else:
                ans.next = l2_ptr
                l2_ptr = l2_ptr.next
            ans = ans.next

        if l1_ptr is not None:
            ans.next = l1_ptr
        else:
            ans.next = l2_ptr

        return guard.next
Пример #11
0
        pre = guard
        while pre.next is not None and pre.next.next is not None:
            l = pre.next
            r = pre.next.next
            print(l.val, r.val)

            l.next = r.next
            r.next = l
            pre.next = r

            pre = l

        return guard.next


if __name__ == "__main__":
    solution = Solution()
    l1 = ListNode(1)
    l2 = ListNode(2)
    l3 = ListNode(3)
    l4 = ListNode(4)
    l1.next = l2
    l2.next = l3
    l3.next = l4

    l = solution.swapPairs(l1)
    while l is not None:
        print(l.val)
        l = l.next
Пример #12
0
                l1_ptr = l1_ptr.next
            else:
                ans.next = l2_ptr
                l2_ptr = l2_ptr.next
            ans = ans.next

        if l1_ptr is not None:
            ans.next = l1_ptr
        else:
            ans.next = l2_ptr

        return guard.next


if __name__ == "__main__":
    solution = Solution()
    node_1 = ListNode(-9)
    node_2 = ListNode(3)
    node_3 = ListNode(5)
    node_4 = ListNode(7)

    node_1.next = node_2

    node_3.next = node_4

    solution = Solution()
    head = solution.mergeTwoLists(node_1, node_3)
    while head is not None:
        print(head.val)
        head = head.next
Пример #13
0
        while fast is not None and fast.next is not None:
            slow = slow.next
            fast = fast.next.next

        l1 = node
        l2 = slow.next
        slow.next = None
        return l1, l2


def stringList(head: ListNode):
    while head is not None:
        print(head.val)
        head = head.next


if __name__ == "__main__":
    node_1 = ListNode(4)
    node_2 = ListNode(1)
    node_3 = ListNode(3)
    node_4 = ListNode(2)
    node_5 = ListNode(7)
    node_1.next = node_2
    node_2.next = node_3
    node_3.next = node_4
    node_4.next = node_5

    solution = Solution()
    head = solution.sortList(node_1)
    stringList(head)
Пример #14
0
输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
'''
from algorithm.leetcode.model import ListNode


class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        jump_1, jump_2 = head, head
        while jump_2 is not None and jump_2.next is not None:
            jump_1 = jump_1.next
            jump_2 = jump_2.next.next

        return jump_1


if __name__ == "__main__":
    node_1 = ListNode(1)
    node_2 = ListNode(2)
    node_3 = ListNode(3)
    node_4 = ListNode(4)
    node_5 = ListNode(5)
    node_1.next = node_2
    #node_2.next = node_3
    #node_3.next = node_4
    #node_4.next = node_5

    solution = Solution()
    print(solution.middleNode(node_1).val)
Пример #15
0
            length += 1
            head = head.next
        k = k % length

        for i in range(0, k):
            post = post.next

        while post.next is not None:
            post = post.next
            pre = pre.next

        post.next = sentinel.next
        sentinel.next = pre.next
        pre.next = None

        return sentinel.next


if __name__ == "__main__":
    l1 = ListNode(0)
    l2 = ListNode(1)
    l3 = ListNode(2)
    l1.next = l2
    l2.next = l3

    solution = Solution()
    l = solution.rotateRight(l1, 1)
    while l is not None:
        print(l.val)
        l = l.next
Пример #16
0
                sum = l1.val
                l1 = l1.next
            elif l2:
                sum = l2.val
                l2 = l2.next

            sum = sum + carry_bit
            carry_bit = 1 if sum > 9 else 0

            node = ListNode(sum % 10)
            ans.next = node
            ans = node

        if carry_bit == 1:
            ans.next = ListNode(1)

        return guard.next


if __name__ == "__main__":
    solution = Solution()

    l1 = ListNode(5)
    l2 = ListNode(5)
    #l2.next = ListNode(9)

    ans = solution.addTwoNumbers(l1, l2)
    while ans is not None:
        print(ans.val)
        ans = ans.next