Ejemplo n.º 1
0
from linkedtools import stringToListNode, prettyPrintLinkedList
class Solution:
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        length = 0
        node = head
        while node:
            node = node.next
            length += 1
        target = length - n
        if target == 0:
            return head.next
        else:
            node = head
            for i in range(target-1):
                node = node.next
            node.next = node.next.next
            return head

a = stringToListNode("[1,2,3,4,5]")
prettyPrintLinkedList(Solution().removeNthFromEnd(a, 5))
Ejemplo n.º 2
0
        len1 = 0
        while c:
            len1 += 1
            c = c.next
        c = l2
        len2 = 0
        while c:
            len2 += 1
            c = c.next
        if len1 < len2:
            l1, l2 = l2, l1
        prettyPrintLinkedList(l1)
        prettyPrintLinkedList(l2)
        res = l1
        while l2:
            bonus, l1.val = divmod(l1.val+l2.val+bonus, 10)
            last = l1
            l1 = l1.next
            l2 = l2.next
        while bonus and l1:
            bonus, l1.val = divmod(l1.val+bonus, 10)
            last = l1
            l1 = l1.next
        if bonus:
            last.next = ListNode(1)
        return res

a = stringToListNode("[5]")
b = stringToListNode("[5]")
prettyPrintLinkedList(Solution().addTwoNumbers(a,b))
        
Ejemplo n.º 3
0
        carry = 0

        # 处理等长部分
        while p1 and p2:
            val = p2.val + p1.val + carry
            p.next = ListNode(val % 10)
            carry = val // 10
            p, p1, p2 = p.next, p1.next, p2.next

        # 处理不等长的部分
        while p1:
            val = p1.val + carry
            p.next = ListNode(val % 10)
            carry = val // 10
            p, p1 = p.next, p1.next
        while p2:
            val = p2.val + carry
            p.next = ListNode(val % 10)
            carry = val // 10
            p, p2 = p.next, p2.next

        if carry > 0:
            p.next = ListNode(carry)

        return headnode.next


a = stringToListNode("[1,8]")
b = stringToListNode("[0]")
prettyPrintLinkedList(Solution().addTwoNumbers(a, b))
Ejemplo n.º 4
0
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        """
        if head is None or head.next is None or m == n:
            return head
        headnode = ListNode(-1)
        headnode.next = head
        anchor = headnode
        for _ in range(m - 1):
            anchor = anchor.next
        a = anchor.next
        b = a.next
        c = b.next

        k = n - m
        for _ in range(k):
            b.next = a
            if c:
                a, b, c = b, c, c.next
            else:
                a, b = b, c
        anchor.next.next = b
        anchor.next = a
        return headnode.next


a = stringToListNode("[1,2,3]")
prettyPrintLinkedList(Solution().reverseBetween(a, 2, 3))
Ejemplo n.º 5
0
from linkedtools import ListNode, prettyPrintLinkedList, stringToListNode


class Solution:
    def oddEvenList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head
        head1 = head
        head2 = head.next
        c1 = head1
        c2 = head2
        while c2 and c2.next:
            c1.next = c2.next
            c1 = c1.next
            c2.next = c1.next
            c2 = c2.next
        c1.next = head2
        return head1


a = stringToListNode("[]")
prettyPrintLinkedList(Solution().oddEvenList(a))
Ejemplo n.º 6
0

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        headnode = ListNode(None)
        headnode.next = head
        delete = False
        p = headnode  # prev
        c = head  # current
        while c:
            while c.next and c.val == c.next.val:
                c.next = c.next.next
                if not delete:
                    delete = True
            if delete:
                c = c.next
                p.next = c
                delete = False
            else:
                c = c.next
                p = p.next
        return headnode.next


a = stringToListNode("[1,1,1,2,2,4]")
prettyPrintLinkedList(Solution().deleteDuplicates(a))
Ejemplo n.º 7
0
        """
        if head is None or head.next is None:
            return head
        sorted = head
        unsorted = head.next
        inf = ListNode(float("inf"))
        sorted.next = inf
        # sorted.next.next = sorted

        while unsorted:
            c = sorted
            while c.val < unsorted.val:
                c = c.next
            temp = unsorted
            unsorted = unsorted.next

            temp.next = c.next
            c.next = temp
            c.val, temp.val = temp.val, c.val

        while True:
            if c.next.val == float("inf"):
                break
            c = c.next
        c.next = None
        return sorted


a = stringToListNode("[6,5,3,1,8,7,2,4]")
# a = stringToListNode("[1,6]")
prettyPrintLinkedList(Solution().insertionSortList(a))