Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- ListNode* partition(ListNode* head, int x) {
- ListNode *dummy = new ListNode(0, head);
- ListNode *partition = new ListNode(0);
- ListNode *curr = dummy, *pcurr = partition;
- while(curr->next){
- if(curr->next->val < x){
- pcurr->next = curr->next;
- pcurr = pcurr->next;
- curr->next = curr->next->next;
- }
- else{
- curr = curr->next;
- }
- }
- pcurr->next = dummy->next;
- return partition->next;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement