Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions reverse-linked-list/wozlsla.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
# Complexity
time : O(N)
space : O(1) / O(N)
"""

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next


class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:

prev = None
curr = head

while curr:
nxt = curr.next
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아~ curr의 next 포인터를 잠시 nxt 에 저장을 해두는 거군요 오..
이렇게 푸니까 훨씬 간단하고 좋네요👍

curr.next = prev
prev = curr
curr = nxt

return prev


""" Stack
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우와 스택으로 푸는방법!! 진짜 대박이네요👍👍👍
스택 친근하긴 하지만 막상 풀때는 잘 생각이 안나는데 너무 좋은 방법 같아요~~


stack = []
node = head

while node:
stack.append(node)
node = node.next

dummy = ListNode(-1)
node = dummy

while stack:
node.next = stack.pop()
node = node.next

node.next = None
return dummy.next
"""