Example #1
0
    def solve(self, head, n):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode

        if n == 0 or not head:
            return head

        fakeHead = SingleLinkedListNode(-1, head)

        cur = fakeHead
        l = 0
        while cur.next:
            l += 1
            cur = cur.next

        n %= l

        quick = fakeHead

        for i in xrange(n):
            if not quick:
                return head
            else:
                quick = quick.next

        slow = fakeHead
        end = quick
        while end.next:
            end = end.next
            slow = slow.next

        end.next = fakeHead.next
        fakeHead.next = slow.next
        slow.next = None

        return fakeHead.next
Example #2
0
    def solve(self, head, n):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode

        if n == 0 or not head:
            return head

        fakeHead = SingleLinkedListNode(-1, head)

        cur = fakeHead
        l = 0
        while cur.next:
            l += 1
            cur = cur.next

        n %= l

        quick = fakeHead

        for i in xrange(n):
            if not quick:
                return head
            else:
                quick = quick.next

        slow = fakeHead
        end = quick
        while end.next:
            end = end.next
            slow = slow.next

        end.next = fakeHead.next
        fakeHead.next = slow.next
        slow.next = None

        return fakeHead.next
Example #3
0
    def solve(self, l1, l2):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        head = None
        plusOne = 0
        while l1 and l2:
            d = l1.val + l2.val + plusOne
            plusOne = d / 10
            d %= 10
            node = SingleLinkedListNode(d)
            if not head:
                head = node
                cur = node
            else:
                cur.next = node
                cur = node
            l1 = l1.next
            l2 = l2.next

        while l1:
            d = l1.val + plusOne
            plusOne = d / 10
            d %= 10
            node = SingleLinkedListNode(d)
            if not head:
                head = node
                cur = node
            else:
                cur.next = node
                cur = node
            l1 = l1.next

        while l2:
            d = l2.val + plusOne
            plusOne = d / 10
            d %= 10
            node = SingleLinkedListNode(d)
            if not head:
                head = node
                cur = node
            else:
                cur.next = node
                cur = node
            l2 = l2.next

        if plusOne:
            node = SingleLinkedListNode(1)
            if not head:
                head = node
            else:
                cur.next = node

        return head
Example #4
0
    def solve(self, head, k):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        fakeHead = SingleLinkedListNode(-1, head)
        last = fakeHead
        while last.next:
            start = last.next
            end = last.next
            for i in xrange(1, k):
                end = end.next
                if not end:
                    return fakeHead.next

            nextHead = end.next

            pre = start
            cur = start.next
            for i in xrange(1, k):
                next = cur.next
                cur.next = pre
                pre = cur
                cur = next

            last.next = end
            start.next = nextHead
            last = start

        return fakeHead.next
Example #5
0
    def mixTwoList(self, l1, l2):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        fakeHead = SingleLinkedListNode(-1)
        cur = fakeHead
        while l1 and l2:
            l1next = l1.next
            l2next = l2.next

            cur.next = l1
            cur = cur.next
            cur.next = l2
            cur = cur.next

            l1 = l1next
            l2 = l2next

        if l1:
            cur.next = l1
            cur = cur.next
        elif l2:
            cur.next = l2
            cur = cur.next

        cur.next = None

        return fakeHead.next
Example #6
0
    def mergeTwo(self, l1, l2):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        if not l1 or not l2:
            return l1 or l2

        fakeHead = SingleLinkedListNode(-1)
        cur = fakeHead

        while l1 and l2:
            if l1.val < l2.val:
                node = l1
                l1 = l1.next
            else:
                node = l2
                l2 = l2.next
            cur.next = node
            cur = node
        while l1:
            cur.next = l1
            cur = l1
            l1 = l1.next
        while l2:
            cur.next = l2
            cur = l2
            l2 = l2.next

        return fakeHead.next
Example #7
0
    def solve(self, head):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        fakeHead = SingleLinkedListNode(-1, head)
        n = 0
        cur = head
        while cur:
            n += 1
            cur = cur.next
        i = 1
        while i <= n:
            pre = fakeHead
            cur = pre.next
            while cur:
                l1 = cur
                for j in xrange(i):
                    if cur:
                        cur = cur.next
                    else:
                        break
                l2 = cur
                for j in xrange(i):
                    if cur:
                        cur = cur.next
                    else:
                        break
                start, end = self.merge(l1, l2, l2, cur)
                pre.next = start
                end.next = cur
                pre = end
            i *= 2

        return fakeHead.next
    def solve(self, head):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode

        if not head:
            return None

        fakeHead = SingleLinkedListNode(-float('inf'), head)
        lastValidNode = fakeHead
        pre = fakeHead
        cur = head
        curVal = head.val
        curCount = 0
        while cur:
            if cur.val == curVal:
                curCount += 1
            else:
                if curCount == 1:
                    lastValidNode.next = pre
                    lastValidNode = pre

                curVal = cur.val
                curCount = 1
            pre = cur
            cur = cur.next

        if curCount == 1:
            lastValidNode.next = pre
            lastValidNode = pre

        lastValidNode.next = None

        return fakeHead.next
Example #9
0
    def solve(self, head, x):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        fakeHead1 = SingleLinkedListNode(0)
        fakeHead2 = SingleLinkedListNode(0)

        cur1 = fakeHead1
        cur2 = fakeHead2

        cur = head
        while cur:
            if cur.val < x:
                cur1.next = cur
                cur1 = cur
            else:
                cur2.next = cur
                cur2 = cur
            cur = cur.next

        cur1.next = fakeHead2.next
        cur2.next = None
        return fakeHead1.next
Example #10
0
    def solve(self, head):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        fakeHead = SingleLinkedListNode(-1, head)

        slow = fakeHead
        quick = fakeHead
        while quick and quick.next:
            slow = slow.next
            quick = quick.next.next
            if slow == quick:
                return True
        return False
Example #11
0
 def solve(self, head):
     from DataStructure.SingleLinkedListNode import SingleLinkedListNode
     fakeHead = SingleLinkedListNode(-1, head)
     pre = fakeHead
     while pre.next and pre.next.next:
         node1 = pre.next
         node2 = pre.next.next
         next = node2.next
         pre.next = node2
         node2.next = node1
         node1.next = next
         pre = node1
     return fakeHead.next
Example #12
0
 def solve(self, head, n):
     from DataStructure.SingleLinkedListNode import SingleLinkedListNode
     fakeHead = SingleLinkedListNode(0, head)
     quick = fakeHead
     for i in xrange(n + 1):
         quick = quick.next
     pre = fakeHead
     cur = head
     while quick:
         pre = cur
         cur = cur.next
         quick = quick.next
     pre.next = cur.next
     return fakeHead.next
Example #13
0
 def solve(self, head):
     from DataStructure.SingleLinkedListNode import SingleLinkedListNode
     fakeHead = SingleLinkedListNode(-1, head)
     slow = fakeHead
     quick = fakeHead
     while quick and quick.next:
         slow = slow.next
         quick = quick.next.next
         if slow == quick:
             meetPoint = slow
             slow = fakeHead
             while slow != quick:
                 slow = slow.next
                 quick = quick.next
             return slow
     return None
    def solve(self, head):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        fakeHead = SingleLinkedListNode(-float('inf'), head)

        pre = fakeHead
        cur = head
        curVal = fakeHead.val

        while cur:
            if cur.val != curVal:
                pre.next = cur
                pre = cur
                curVal = cur.val
            cur = cur.next

        # important don't forget this
        pre.next = None

        return fakeHead.next
Example #15
0
    def solve(self, head):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        fakeHead = SingleLinkedListNode(-1, head)
        slow = fakeHead
        quick = fakeHead
        while quick and quick.next:
            slow = slow.next
            quick = quick.next.next

        pre = None
        cur = slow.next
        slow.next = None
        while cur:
            next = cur.next
            cur.next = pre
            pre = cur
            cur = next
        end = pre
        head = fakeHead.next
        self.mixTwoList(head, end)
Example #16
0
    def solve(self, head, m, n):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode
        fakeHead = SingleLinkedListNode(-1, head)
        pre = fakeHead
        cur = head
        i = 1
        while i < m:
            pre = cur
            cur = cur.next
            i += 1
        mth_pre = pre

        while i < (n + 1):
            next = cur.next
            cur.next = pre
            pre = cur
            cur = next
            i += 1

        mth_pre.next.next = cur
        mth_pre.next = pre

        return fakeHead.next
Example #17
0
    def solve(self, head):
        from DataStructure.SingleLinkedListNode import SingleLinkedListNode

        fakeHead = SingleLinkedListNode(-1, head)

        pre = fakeHead
        cur = head
        while cur:
            preNode = fakeHead
            node = fakeHead.next

            while node.val < cur.val:
                preNode = node
                node = node.next

            if node != cur:
                pre.next = cur.next
                preNode.next = cur
                cur.next = node
                cur = pre

            pre = cur
            cur = cur.next
        return fakeHead.next