File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ // TC: O(n)
2
+ // -> find middle, reverse, merge -> max O(n)
3
+ // SC: O(1)
4
+ class Solution {
5
+ public void reorderList (ListNode head ) {
6
+ if (head == null || head .next == null ) return ;
7
+
8
+ ListNode slow = head , fast = head ;
9
+ while (fast != null && fast .next != null ) {
10
+ slow = slow .next ;
11
+ fast = fast .next .next ;
12
+ }
13
+
14
+ ListNode backSide = null ;
15
+ ListNode curr = slow .next ;
16
+ slow .next = null ;
17
+
18
+ while (curr != null ) {
19
+ ListNode temp = curr .next ;
20
+ curr .next = backSide ;
21
+ backSide = curr ;
22
+ curr = temp ;
23
+ }
24
+
25
+ ListNode first = head ;
26
+ ListNode second = backSide ;
27
+ while (second != null ) {
28
+ ListNode temp = first .next ;
29
+ first .next = second ;
30
+ first = second ;
31
+ second = temp ;
32
+ }
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments