# 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
"""
smallHead=ListNode(0)
smallCur=smallHead
bigHead=ListNode(0)
bigCur=bigHead
while(head!=None):
if head.val<x:
smallCur.next=ListNode(head.val)
smallCur=smallCur.next
else:
bigCur.next=ListNode(head.val)
bigCur=bigCur.next
head=head.next
if smallHead.next==None:
return bigHead.next
smallCur.next=bigHead.next
return smallHead.next
LeetCode-86-Partition List 链表
最新推荐文章于 2021-02-23 21:38:10 发布