美文网首页
86. Partition List

86. Partition List

作者: April63 | 来源:发表于2018-06-19 12:41 被阅读0次

有一个地方没有考虑到,就是big的表需要在末尾指向None,否则就会time limit出错
代码如下:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def partition(self, head, x):
        """
        :type head: ListNode
        :type x: int
        :rtype: ListNode
        """
        s = small = ListNode(-1)
        b = big = ListNode(-1)
        h = head
        while h:
            if h.val < x:
                s.next = h
                s = s.next
                h = h.next
            else:
                b.next = h
                b = b.next
                h = h.next
        b.next = None
        s.next = big.next
        return small.next

相关文章

网友评论

      本文标题:86. Partition List

      本文链接:https://www.haomeiwen.com/subject/gvdaeftx.html