File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ public class Solution {
2+ public void reorderList (ListNode head ) {
3+ if (head == null || head .next == null ) return ;
4+
5+ ListNode backward = head ;
6+ ListNode forward = head ;
7+ while (forward != null && forward .next != null ) {
8+ backward = backward .next ;
9+ forward = forward .next .next ;
10+ }
11+
12+ ListNode curr = backward .next ;
13+ backward .next = null ;
14+
15+ ListNode prev = null ;
16+ while (curr != null ) {
17+ ListNode tempNext = curr .next ;
18+ curr .next = prev ;
19+ prev = curr ;
20+ curr = tempNext ;
21+ }
22+
23+ ListNode first = head ;
24+ ListNode second = prev ;
25+
26+ while (second != null ) {
27+ ListNode firstNext = first .next ;
28+ ListNode secondNext = second .next ;
29+
30+ first .next = second ;
31+ second .next = firstNext ;
32+
33+ first = firstNext ;
34+ second = secondNext ;
35+ }
36+ }
37+ }
38+
You can’t perform that action at this time.
0 commit comments