File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ 투포인터 기법을 이용해 중간 노드를 구해 리스트를 반으로 나누고 뒤의 그룹은 순서를 뒤집는다
4
+ 번갈아가면서 다시 붙여준다
5
+
6
+ 노드 총 개수 : N
7
+
8
+ TC : O(N)
9
+
10
+ SC : O(1)
11
+ */
12
+
13
+ class Solution {
14
+ public:
15
+ void reorderList (ListNode* head) {
16
+ ListNode *slow = head, *fast = head;
17
+
18
+ while (fast && fast->next ) {
19
+ slow = slow->next ;
20
+ fast = fast->next ->next ;
21
+ }
22
+
23
+ ListNode *prev = nullptr , *curr = slow->next ;
24
+ slow->next = nullptr ;
25
+
26
+ while (curr) {
27
+ ListNode *tmp = curr->next ;
28
+ curr->next = prev;
29
+ prev = curr;
30
+ curr = tmp;
31
+ }
32
+
33
+ ListNode *first = head, *second = prev;
34
+ while (second) {
35
+ ListNode *tmp1 = first->next , *tmp2 = second->next ;
36
+ first->next = second;
37
+ second->next = tmp1;
38
+ first = tmp1;
39
+ second = tmp2;
40
+ }
41
+ }
42
+ };
43
+
44
+ struct ListNode {
45
+ int val;
46
+ ListNode *next;
47
+ ListNode () : val(0 ), next(nullptr ) {}
48
+ ListNode (int x) : val(x), next(nullptr ) {}
49
+ ListNode (int x, ListNode *next) : val(x), next(next) {}
50
+ };
You can’t perform that action at this time.
0 commit comments