leetcode -- Partition List -- 常见题型重点

https://leetcode.com/problems/partition-list/

“穿针引线”法。 构造两个dummy,然后穿针引线。

参考http://www.cnblogs.com/zuoyuan/p/3783276.html

class Solution(object):
    def partition(self, head, x):
        """ :type head: ListNode :type x: int :rtype: ListNode """
        dummy1, dummy2 = ListNode(0), ListNode(0)

        p1, p2 = dummy1,dummy2
        p1.next, p2.next = None, None
        i = head

        while i:
            tmp = i.next
            if i.val < x:
                p1.next = i
                p1 = p1.next
                p1.next = None
            else:
                p2.next = i
                p2 = p2.next
                p2.next = None

            i = tmp

        p1.next = dummy2.next
        return dummy1.next

你可能感兴趣的:(LeetCode)