题目:
思路:
比X大的元素存一个链表,比X小的存一个链表.
然后把小的链表放在大的链表前面
代码:
# 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:
ans=ListNode(0)
res=ListNode(0)
nodemax=ans
nodemin=res
while head:
if head.val<x:
nodemin.next=head
nodemin=nodemin.next
#nodemin.next=None
else:
nodemax.next=head
nodemax=nodemax.next
# nodemax.next=None
head=head.next
nodemax.next=None
nodemin.next=ans.next
return res.next