-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0143-Reorder-list.cs
More file actions
49 lines (41 loc) · 1.14 KB
/
0143-Reorder-list.cs
File metadata and controls
49 lines (41 loc) · 1.14 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
using Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0143.Reorder_list
{
public class _0143_Reorder_list
{
public void ReorderList(ListNode head)
{
if (head == null) return;
var temp = head;
Stack<ListNode> stack = new Stack<ListNode>();
int count = 0;
while (temp != null)
{
var node = new ListNode() { val = temp.val };
stack.Push(node);
temp = temp.next;
count++;
}
var curr = head;
int mid = count / 2;
while (curr != null && stack.Count > mid)
{
var node = stack.Pop();
node.next = curr.next;
curr.next = node;
if (stack.Count > mid)
curr = curr.next.next;
else
{
if (count % 2 == 0)
curr.next.next = null;
else
curr.next = null;
}
}
}
}
}