# 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:
#双链表法
pmin = node1 = ListNode(0)
pmax = node2 = ListNode(0)
while head:
if head.val < x:
pmin.next = head
pmax = pmin.next
else:
pmax.next = head
pmax = pmax.next
head = head.next
pmin.next = node2
pmax.next = None
return node1
面试题 02.04. 分割链表
最新推荐文章于 2025-05-10 16:26:05 发布