链表分割
链表分割_牛客网
https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70?tpId=8&tqId=11004&rp=1&ru=/ta/cracking-the-coding-interview&qru=/ta/cracking-the-coding-interview/question-ranking
/*
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
