-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionList.cpp
More file actions
executable file
·52 lines (44 loc) · 1.08 KB
/
PartitionList.cpp
File metadata and controls
executable file
·52 lines (44 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// PartitionList.cpp
// leetcode
//
// Created by witwolf on 4/24/15.
// Copyright (c) 2015 witwolf. All rights reserved.
//
#include <iostream>
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* partition(struct ListNode* head, int x) {
struct ListNode* prev = NULL,*node = head,*guaid = NULL,*guaid_prev = NULL,*next=NULL;
while (node) {
next = node->next;
if(guaid != NULL && node->val < x){
if(guaid == head){
head = node;
head->next = guaid;
guaid_prev = head;
}else{
guaid_prev->next = node;
guaid_prev = node;
guaid_prev->next = guaid;
}
if(prev){
prev->next = next;
}
node = next;
continue;
}
if(guaid == NULL && node->val >= x){
guaid_prev = prev;
guaid = node;
}
prev = node;
node = next;
}
return head;
}