R3-双指针(快慢指针)
新建两个链表
一个链表记录<x的值
一个链表记录>=x的值
拼接即可
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:#新建链表以及指针表示low_dummy,high_dummy=ListNode(0),ListNode(0)low,high=low_dummy,high_dummywhile head:if head.val<x:low.next=headlow=low.nextelse:high.next=headhigh=high.nexthead=head.next#拼接low.next=high_dummy.nexthigh.next=Nonereturn low_dummy.next
ps:
#新建链表以及指针表示low_dummy,high_dummy=ListNode(0),ListNode(0)low,high=low_dummy,high_dummy