File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Constraints:
3
+ - The number of nodes in the list is the range [0, 5000]
4
+ - -5000 <= Node.val <= 5000
5
+
6
+ Time Complexity: O(n)
7
+ - n์ linked list์ ๋
ธ๋ ์
8
+ - ๋ฆฌ์คํธ๋ฅผ ํ ๋ฒ ์ํํ๋ฉด์ ๊ฐ ๋
ธ๋๋ฅผ ํ ๋ฒ์ฉ๋ง ๋ฐฉ๋ฌธํ๊ธฐ ๋๋ฌธ
9
+
10
+ Space Complexity: O(1)
11
+ - ์ถ๊ฐ ๊ณต๊ฐ์ผ๋ก prev, curr, temp ์ธ ๊ฐ์ ํฌ์ธํฐ๋ง ์ฌ์ฉ
12
+ - ์
๋ ฅ ํฌ๊ธฐ์ ๊ด๊ณ์์ด ์ผ์ ํ ์ถ๊ฐ ๊ณต๊ฐ๋ง ์ฌ์ฉ
13
+
14
+ ํ์ด ๋ฐฉ๋ฒ:
15
+ 1. ์ธ ๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉด์ ๋ค์ง๊ธฐ
16
+ - prev: ์ด์ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ
17
+ - curr: ํ์ฌ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ
18
+ - temp: ๋ค์ ๋
ธ๋๋ฅผ ์์ ์ ์ฅํ๋ ํฌ์ธํฐ
19
+
20
+ 2. ๊ฐ ๋จ๊ณ์์:
21
+ - ๋ค์ ๋
ธ๋ ์์ ์ ์ฅ (temp)
22
+ - ํ์ฌ ๋
ธ๋์ next๋ฅผ ์ด์ ๋
ธ๋๋ก ๋ณ๊ฒฝ
23
+ - ํฌ์ธํฐ๋ค์ ํ ์นธ์ฉ ์ ์ง
24
+
25
+ 3. ์ฐธ๊ณ :
26
+ - ํฌ์ธํฐ๋ค์ ์ด๋ ์์๊ฐ ์ค์
27
+ - prev๊ฐ ์๋ก์ด head๊ฐ ๋จ
28
+ """
29
+
30
+ # Definition for singly-linked list.
31
+ # class ListNode:
32
+ # def __init__(self, val=0, next=None):
33
+ # self.val = val
34
+ # self.next = next
35
+ class Solution :
36
+ def reverseList (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
37
+ prev = None
38
+ curr = head
39
+
40
+ while curr is not None :
41
+
42
+ temp = curr .next
43
+ curr .next = prev
44
+ prev = curr
45
+ curr = temp
46
+
47
+ return prev
You canโt perform that action at this time.
0 commit comments