File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, val=0, next=None):
4+ # self.val = val
5+ # self.next = next
6+ class Solution :
7+ def reorderList (self , head : Optional [ListNode ]) -> None :
8+ """
9+ Do not return anything, modify head in-place instead.
10+ """
11+ # 시간복잡도 o(n), 공간복잡도 O(1)
12+
13+ # 리스트 중간 지점 찾기
14+ slow , fast = head , head
15+
16+ while fast and fast .next :
17+ slow = slow .next
18+ fast = fast .next .next
19+
20+ # 중간에서 뒷부분 뒤집기
21+ curr = slow .next
22+ prev = None
23+ slow .next = None
24+
25+ while curr :
26+ next_temp = curr .next
27+ curr .next = prev
28+ prev = curr
29+ curr = next_temp
30+
31+ # 앞부분과 뒷부분 합치기
32+ first , second = head , prev
33+
34+ while second :
35+ temp1 = first .next
36+ temp2 = second .next
37+
38+ first .next = second
39+ second .next = temp1
40+
41+ first = temp1
42+ second = temp2
You can’t perform that action at this time.
0 commit comments