We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent da62e13 commit 629c00cCopy full SHA for 629c00c
reverse-linked-list/yyyyyyyyyKim.py
@@ -0,0 +1,19 @@
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 reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
8
+
9
+ # 시간복잡도 O(n), 공간복잡도 O(1)
10
+ answer = None
11
12
+ while head:
13
+ next_node = head.next # 다음 노드 저장
14
+ head.next = answer # 현재 노드의 next를 이전 노드로 변경
15
+ answer = head # answer를 현재 노드로 업데이트
16
+ head = next_node # head를 다음 노드로 이동
17
18
+ # answer = 역순 리스트의 head
19
+ return answer
0 commit comments