File tree 2 files changed +79
-0
lines changed
LinkedList/LinkedListPartition/python
2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * struct ListNode *next;
6
+ * };
7
+ */
8
+
9
+
10
+ struct ListNode * partition (struct ListNode * head , int x ){
11
+
12
+ struct ListNode * small , * large ;
13
+ struct ListNode * p = head ;
14
+ struct ListNode * s ,* l ;
15
+ small = large = NULL ;
16
+
17
+ if (p == NULL )
18
+ {
19
+ return p ;
20
+ }
21
+ else {
22
+ while (p != NULL )
23
+ {
24
+ if (p -> val < x )
25
+ {
26
+ if (small != NULL )
27
+ {
28
+ small -> next = p ;
29
+ p = p -> next ;
30
+ small = small -> next ;
31
+ small -> next = NULL ;
32
+ }
33
+ else {
34
+ small = p ;
35
+ p = p -> next ;
36
+ small -> next = NULL ;
37
+ s = small ;
38
+ }
39
+ }
40
+ else {
41
+ if (large != NULL )
42
+ {
43
+ large -> next = p ;
44
+ p = p -> next ;
45
+ large = large -> next ;
46
+ large -> next = NULL ;
47
+ }
48
+ else {
49
+ large = p ;
50
+ p = p -> next ;
51
+ large -> next = NULL ;
52
+ l = large ;
53
+ }
54
+ }
55
+ }
56
+ }
57
+ if (large == NULL )
58
+ {
59
+ return head ;
60
+ }
61
+ else if (small == NULL ){
62
+ return head ;
63
+ }
64
+ else {
65
+ small -> next = l ;
66
+ head = s ;
67
+ return head ;
68
+ }
69
+ }
Original file line number Diff line number Diff line change
1
+ # Partition List
2
+
3
+ Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
4
+
5
+ You should preserve the original relative order of the nodes in each of the two partitions.
6
+
7
+ ## Example
8
+
9
+ `Input: head = 1->4->3->2->5->2, x = 3
10
+ Output: 1->2->2->4->3->5`
You can’t perform that action at this time.
0 commit comments