/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ class Partition { public: ListNode* partition(ListNode* pHead, int x) { // write code here if(pHead==nullptr) return nullptr; ListNode *smallHead=new ListNode(0); ListNode *bigHead=new ListNode(0); ListNode *small=smallHead; ListNode *big=bigHead; while(pHead){ if(pHead->val<x){ small->next=pHead; small=small->next; } else{ big->next=pHead; big=big->next; } pHead=pHead->next; } big->next=nullptr; small->next=bigHead->next; return smallHead->next; } };
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Partition: def partition(self, pHead, x): # write code here smallHead=ListNode(0) bigHead=ListNode(0) small=smallHead big=bigHead while pHead!=None: if pHead.val<x: small.next=pHead small=small.next else: big.next=pHead big=big.next pHead=pHead.next big.next=None small.next=bigHead.next return smallHead.next