-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path143.ReorderList.cs
55 lines (52 loc) · 1.26 KB
/
143.ReorderList.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Solution {
public void ReorderList(ListNode head) {
var mid = FindMiddleNode(head);
if (mid == null || mid.next == null) return;
var head2 = mid.next;
mid.next = null;
head2 = ReverseList(head2);
MergeList(head, head2);
}
private ListNode FindMiddleNode(ListNode head)
{
var last = head;
var mid = head;
while (last != null)
{
last = last.next;
if (last != null)
{
last = last.next;
mid = mid.next;
}
}
return mid;
}
private ListNode ReverseList(ListNode head)
{
var current = head;
head = null;
while (current != null)
{
var next = current.next;
current.next = head;
head = current;
current = next;
}
return head;
}
private void MergeList(ListNode head1, ListNode head2)
{
var p1 = head1;
var p2 = head2;
while (p2 != null)
{
var p1Next = p1.next;
var p2Next = p2.next;
p1.next = p2;
p2.next = p1Next;
p1 = p1Next;
p2 = p2Next;
}
}
}