Ejemplo n.º 1
0
    def insert(self, value, index):

        insert_node = ListNode(value)

        current_index = 0
        current_node = self.head

        while (current_index < index - 1):
            current_index += 1
            current_node = current_node.next_node
            print current_node.value

        insert_node.next_node = current_node.next_node
        current_node.next_node = insert_node
    def splitListToParts(self, head: Optional[ListNode],
                         k: int) -> List[Optional[ListNode]]:
        ans: List[ListNode] = []
        cur = head
        allcount = 0
        while cur:
            allcount += 1
            cur = cur.next

        eachcount = allcount // k
        remainder = allcount - eachcount * k

        cur = ListNode(0)
        cur.next = head
        for i in range(k):
            if eachcount + remainder == 0:
                ans.append(None)
            else:
                tmp = eachcount
                while cur.next and tmp > 0:
                    cur = cur.next
                    tmp -= 1
                if remainder > 0:
                    if cur.next:
                        cur = cur.next
                    remainder -= 1

                temp = cur.next
                cur.next = None
                ans.append(head)
                head = temp
                cur = ListNode(0)
                cur.next = head  # ensure cur is the tail element

        return ans
Ejemplo n.º 3
0
    def addTwoNumbers(self, l1: Optional[ListNode],
                      l2: Optional[ListNode]) -> Optional[ListNode]:
        prev = ListNode(0)
        cur = prev
        carry = 0
        while l1 or l2:
            v1 = l1.val if l1 else 0
            v2 = l2.val if l2 else 0
            v = v1 + v2 + carry
            v0 = v % 10
            carry = v // 10
            tmp = ListNode(v0)
            cur.next = tmp
            cur = cur.next

            l1 = l1.next if l1 else None
            l2 = l2.next if l2 else None

        if carry > 0:
            tmp = ListNode(carry)
            cur.next = tmp

        return prev.next
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        count = 0
        ltmp: ListNode = head
        while count < k and ltmp:
            ltmp = ltmp.next
            count += 1
        if count < k:
            return head

        cur: ListNode = self.reverseKGroup(ltmp, k)

        while count > 0:
            ltmp = head.next
            head.next = cur
            cur = head
            head = ltmp
            count -= 1

        return cur
        def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode:
            lret = ListNode(0)
            ll, lr = l1, l2
            lptr = lret
            while ll and lr:
                if ll.val <= lr.val:
                    lptr.next = ll
                    ll = ll.next
                else:
                    lptr.next = lr
                    lr = lr.next
                lptr = lptr.next

            if ll:
                lptr.next = ll
            else:
                lptr.next = lr

            return lret.next
Ejemplo n.º 6
0
        def mergeList(l1: ListNode, l2: ListNode) -> ListNode:
            tmp = ListNode(0)
            sortlist = tmp
            while l1 and l2:
                if l1.val < l2.val:
                    tmp.next = l1
                    l1 = l1.next
                    tmp = tmp.next
                else:
                    tmp.next = l2
                    l2 = l2.next
                    tmp = tmp.next
            if l1:
                tmp.next = l1
            elif l2:
                tmp.next = l2

            return sortlist.next
Ejemplo n.º 7
0
 def prepend(self, value):
     new_node = ListNode(value)
     new_node.next_node = self.head
     self.head = new_node