有一个地方没有考虑到,就是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
网友评论