-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path086-PartitionList.cs
36 lines (34 loc) · 1 KB
/
086-PartitionList.cs
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
//-----------------------------------------------------------------------------
// Runtime: 148ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _086_PartitionList
{
public ListNode Partition(ListNode head, int x)
{
var lessThanHead = new ListNode(-1);
var greaterThanHead = new ListNode(-1);
ListNode p = head, lessP = lessThanHead, greaterP = greaterThanHead;
while (p != null)
{
if (p.val < x)
{
lessP.next = p;
lessP = lessP.next;
}
else
{
greaterP.next = p;
greaterP = greaterP.next;
}
p = p.next;
}
lessP.next = greaterThanHead.next;
greaterP.next = null;
return lessThanHead.next;
}
}
}