File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ # Time Complexity: O(n)
3+ # Space Complexity: O(1)
4+ # Solution
5+ Dale 님의 솔루션을 참고했습니다.
6+ */
7+
8+ /**
9+ * Definition for singly-linked list.
10+ * public class ListNode {
11+ * int val;
12+ * ListNode next;
13+ * ListNode() {}
14+ * ListNode(int val) { this.val = val; }
15+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16+ * }
17+ */
18+ class Solution {
19+ public void reorderList (ListNode head ) {
20+ ListNode slow = head ;
21+ ListNode fast = head ;
22+ while (fast != null && fast .next != null ) {
23+ slow = slow .next ;
24+ fast = fast .next .next ;
25+ }
26+
27+ ListNode curr = slow .next ;
28+ slow .next = null ;
29+
30+ ListNode prev = null ;
31+ while (curr != null ) {
32+ ListNode tempNext = curr .next ;
33+ curr .next = prev ;
34+ prev = curr ;
35+ curr = tempNext ;
36+ }
37+
38+ ListNode left = head ;
39+ ListNode right = prev ;
40+ while (right != null ) {
41+ ListNode leftNext = left .next ;
42+ ListNode rightNext = right .next ;
43+ left .next = right ;
44+ right .next = leftNext ;
45+ left = leftNext ;
46+ right = rightNext ;
47+ }
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments