Skip to content

Commit 9d7f9ed

Browse files
committed
Solution Week11: reorderList
1 parent f805dc5 commit 9d7f9ed

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

reorder-list/juhui-jeogn.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
*/
4+
class Solution {
5+
public void reorderList(ListNode head) {
6+
List<ListNode> list = new ArrayList<>();
7+
8+
while(head != null) {
9+
list.add(head);
10+
head= head.next;
11+
}
12+
13+
int i = 0;
14+
int j = list.size() -1;
15+
while(i < j) {
16+
ListNode left = list.get(i);
17+
ListNode right = list.get(j);
18+
19+
left.next = right;
20+
i++;
21+
22+
if (i == j) {
23+
right.next = null;
24+
break;
25+
}
26+
27+
right.next = list.get(i);
28+
j--;
29+
}
30+
31+
list.get(i).next = null;
32+
}
33+
}

0 commit comments

Comments
 (0)