题目描述
解题
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
p = ListNode(0)
q = ListNode(0)
n = p
m = q
r = head
while r!=None:
if r.val < x:
p.next = r
p = p.next
elif r.val >= x:
q.next = r
q = q.next
r = r.next
q.next = None #否则该链表有环
p.next = m.next
return n.next