示例#1
0
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """

        fast, slow = head, head
        nhead = ListNode(None)
        if not fast or not fast.next: return True

        while fast and fast.next:

            fast = fast.next.next

            tmp = slow
            slow = slow.next

            tmp.next = nhead.next
            nhead.next = tmp

        npt = nhead.next

        if not fast: opt = slow
        else: opt = slow.next

        while npt:
            if npt.val != opt.val:
                return False
            npt, opt = npt.next, opt.next

        return True
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """

        hd = ListNode(None)
        hd.next, lpt, fpt = head, hd, hd
        cnt = k

        while cnt and lpt.next:
            lpt = lpt.next
            cnt -= 1

        # Handles case when k is greater than the length of the linkedlist
        if not lpt.next and cnt != k:

            n = k - cnt
            cnt = k % n
            lpt = hd
            for _ in xrange(cnt):
                lpt = lpt.next

        while lpt.next:
            lpt = lpt.next
            fpt = fpt.next

        lpt.next = hd.next
        hd.next = fpt.next
        fpt.next = None

        return hd.next
示例#3
0
def to_linkedlist(lst):
    root = ListNode(None)
    pt = root

    for item in lst:
        pt.next = ListNode(item)
        pt = pt.next

    return root.next
示例#4
0
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        fpt = h = ListNode(None)
        lpt = head.next if head else None
        stack = [head]

        while lpt:

            if lpt.val == stack[-1].val:
                stack.append(lpt)
            else:
                if len(stack) == 1:
                    fpt.next = stack.pop()
                    fpt = fpt.next
                else:
                    while stack:
                        stack.pop()
                stack.append(lpt)
            # print([x.val for x in stack])
            # print(h.to_str())
            lpt = lpt.next

        if len(stack) == 1:
            fpt.next = stack.pop()
        else:
            fpt.next = None

        return h.next
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        pt = head
        rhead = ListNode(None)

        while pt:
            tmp = rhead.next
            rhead.next = pt

            pt = pt.next
            rhead.next.next = tmp

        return rhead.next
示例#6
0
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        curval = None
        pre = ListNode(curval)
        pt = head

        while pt:
            if pt.val != curval:
                pre.next = pt
                pre = pt
                curval = pt.val
            pt = pt.next
        pre.next = None  # handling the last point

        return head
    def partition(self, head, x):
        """
        :type head: ListNode
        :type x: int
        :rtype: ListNode
        """

        lhead, ghead = ListNode(None), ListNode(None)
        lhead.next = head

        lpt, gpt = lhead, ghead

        while lpt.next:
            if lpt.next.val >= x:
                gpt.next = lpt.next
                lpt.next = lpt.next.next

                gpt = gpt.next
                gpt.next = None
            else:
                lpt = lpt.next

        lpt.next = ghead.next
        return lhead.next
    def merge(self, left, right):

        head = ListNode(None)
        pt = head

        while left and right:
            if left.val > right.val:
                pt.next = right
                right = right.next
            else:
                pt.next = left
                left = left.next
            pt = pt.next

        if left: pt.next = left
        if right: pt.next = right

        return head.next
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """

        import heapq
        hq, head = [], ListNode(None)

        for node in lists:
            if node:
                heapq.heappush(hq, (node.val, node))

        pt = head
        while hq:
            pt.next = heapq.heappop(hq)[1]
            pt = pt.next
            if pt.next:
                heapq.heappush(hq, (pt.next.val, pt.next))

        return head.next