Skip to content

Commit 629c00c

Browse files
committed
reverse-linked-list solution
1 parent da62e13 commit 629c00c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

reverse-linked-list/yyyyyyyyyKim.py

+19
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)