def reverseKGroup(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if not head or not head.next or k <= 1:
            #return head
            return LinkedList(head).values()

        dummy = ListNode(None)
        dummy.next = head
        gprev = dummy
        while gprev.next:
            tmp = gprev
            for i in xrange(k):
                tmp = tmp.next
                if not tmp:
                    #return dummy.next
                    return LinkedList(dummy.next).values()
            gtail, pc = gprev.next, gprev.next.next
            for i in xrange(k - 1):
                gprev.next, pc.next, gtail.next = pc, gprev.next, pc.next
                pc = gtail.next
            gprev = gtail
        #return dummy.next
        return LinkedList(dummy.next).values()
예제 #2
0
    def reverseBetween(self, head, m, n):
        """
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        """
        if not head or not head.next or m == n:
            return head
        
        dummy = ListNode(None)
        dummy.next = head
        prev = dummy
        for _ in xrange(m-1):
            prev = prev.next
        curr = prev.next    
        
        revprev, revhead = prev, curr
        prev, curr = curr, curr.next
        
        for _ in xrange(n-m):
            curr.next, prev, curr = prev, curr, curr.next

        revhead.next = curr
        revprev.next = prev
        return dummy.next
    def reverseKGroup(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if k <= 1:
            return head

        dummy = ListNode(None)
        dummy.next = head
        prev = dummy
        while True:
            h = prev
            for i in xrange(k):
                h = h.next
                if not h:
                    return dummy.next

            t = p = prev.next
            node = p.next
            for i in xrange(k - 1):
                next_node = node.next
                node.next, p, node = p, node, next_node
            prev.next, t.next = p, next_node
            prev = t
 def swapPairs(self, head):
     """
     :type head: ListNode
     :rtype: ListNode
     """
     dummy = ListNode(None)
     dummy.next = head
     prev = dummy
     while prev.next and prev.next.next:
         first, second = prev.next, prev.next.next
         prev.next, second.next, first.next = second, first, second.next
         prev = first
     return dummy.next
예제 #5
0
 def removeElements(self, head, val):
     """
     :type head: ListNode
     :type val: int
     :rtype: ListNode
     """
     dummy = ListNode(None)
     dummy.next = head
     node = dummy
     while node.next:
         if node.next.val == val:
             node.next.next, node.next = None, node.next.next
         else:
             node = node.next
     return dummy.next
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        head_values = sorted([(p, p.val) for p in lists if p],
                             key=lambda x: x[1],
                             reverse=True)
        if not head_values:
            return None

        dummy = ListNode(None)
        curr = dummy
        while len(head_values) > 1:
            if head_values[-1][1] <= head_values[-2][1]:
                curr.next = head_values[-1][0]
                curr = curr.next
                if head_values[-1][0].next:
                    head_values[-1] = (head_values[-1][0].next,
                                       head_values[-1][0].next.val)
                else:
                    head_values.pop()
            else:
                head_values.sort(key=lambda x: x[1], reverse=True)

        curr.next = head_values[0][0]
        #return dummy.next
        return LinkedList(dummy.next).values()
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next:
            return head

        curr, ref, count = head, head.val, 1
        dummy = ListNode(None)
        h = dummy
        while curr.next:
            prev, curr = curr, curr.next
            if curr.val != ref:
                if count == 1:
                    h.next = prev
                    h = h.next
                    h.next = None
                ref = curr.val
                count = 1
            else:
                count += 1
        if count == 1:
            h.next = curr
        return dummy.next
예제 #8
0
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        prev = dummy = ListNode(None)
        node1, node2, c = l1, l2, 0
        while node1 or node2:
            n1, node1 = (node1.val, node1.next) if node1 else (0, None)
            n2, node2 = (node2.val, node2.next) if node2 else (0, None)
            c, val = divmod(n1 + n2 + c, 10)
            prev.next = ListNode(val)
            prev = prev.next
        if c:
            prev.next = ListNode(c)

        return dummy.next
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next:
            #return head
            return None if not head else [head.val]

        dummy = ListNode(None)
        dummy.next = head
        prev, left, right = dummy, head, head.next
        while left and right:
            pnext = right.next
            prev.next, right.next, left.next = right, left, right.next
            prev, left = left, pnext
            right = left.next if left else None
        #return dummy.next
        return LinkedList(dummy.next).values()
예제 #10
0
 def addTwoNumbers(self, l1, l2):
     """
     :type l1: ListNode
     :type l2: ListNode
     :rtype: ListNode
     """
     p1, p2 = l1, l2
     head = ListNode(0)
     prev, node = None, head
     while p1 or p2 or node.val >= 10:
         n1 = p1.val if p1 else 0
         n2 = p2.val if p2 else 0
         q, r = divmod(n1 + n2 + node.val, 10)
         node.val = r
         node.next = ListNode(q)
         p1 = p1.next if p1 else None
         p2 = p2.next if p2 else None
         prev, node = node, node.next
     if node.val == 0 and prev:
         prev.next = None
     return head
예제 #11
0
    def partition(self, head, x):
        """
        :type head: ListNode
        :type x: int
        :rtype: ListNode
        """
        small = dummy_small = ListNode(None)
        large = dummy_large = ListNode(None)

        node = head
        while node:
            if node.val < x:
                small.next = node
                small = small.next
            else:
                large.next = node
                large = large.next
            node = node.next

        small.next = dummy_large.next
        large.next = None
        return dummy_small.next
예제 #12
0
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        dummy = ListNode(None)
        dummy.next = head

        fast = head
        for _ in xrange(n):
            fast = fast.next

        slow = dummy
        while fast:
            fast = fast.next
            slow = slow.next

        discarded = slow.next
        slow.next = discarded.next
        discarded.next = None

        return dummy.next
        def mergetwo(l1, l2):
            dummy = ListNode(None)
            curr = dummy
            while l1 and l2:
                if l1.val <= l2.val:
                    curr.next, l1 = l1, l1.next
                else:
                    curr.next, l2 = l2, l2.next
                curr = curr.next

            if not l1:
                curr.next = l2
            else:
                curr.next = l1
            return dummy.next
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        dummy = ListNode(None)
        curr = dummy
        while l1 and l2:
            if l1.val <= l2.val:
                curr.next, l1 = l1, l1.next
            else:
                curr.next, l2 = l2, l2.next
            curr = curr.next

        if not l1:
            curr.next = l2
        else:
            curr.next = l1
        return dummy.next
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     lists = [p for p in lists if p]
     if not lists:
         return None
     values = [p.val for p in lists]
     dummy = ListNode(None)
     curr = dummy
     while len(lists) > 1:
         ind = values.index(min(values))
         curr.next = lists[ind]
         curr = curr.next
         if not lists[ind].next:
             lists.pop(ind)
             values.pop(ind)
         else:
             lists[ind] = lists[ind].next
             values[ind] = lists[ind].val
     curr.next = lists[0]
     #return dummy.next
     return LinkedList(dummy.next).values()