Skip to content

Commit f651ab1

Browse files
committed
Reverse Linked List Solution
1 parent ed2b293 commit f651ab1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

reverse-linked-list/naringst.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# Runtime: 39ms, Memory: 17.88MB
3+
# Time complexity: O(len(head))
4+
# Space complexity: O(len(head))
5+
6+
7+
# Definition for singly-linked list.
8+
# class ListNode:
9+
# def __init__(self, val=0, next=None):
10+
# self.val = val
11+
# self.next = next
12+
13+
class Solution:
14+
def __init__(self):
15+
self.nodes = [] # ListNode 객체를 저장할 배열
16+
17+
def reverseList(self, head: Optional[ListNode]) -> List[Optional[ListNode]]:
18+
prev = None
19+
curr = head
20+
21+
while curr is not None:
22+
nextNode = curr.next
23+
curr.next = prev
24+
prev = curr
25+
curr = nextNode
26+
27+
return prev

0 commit comments

Comments
 (0)