Skip to content

Commit

Permalink
Day 14: Attempt 2: 92_Reverse_Linked_List_II
Browse files Browse the repository at this point in the history
  • Loading branch information
rjsnh1522 committed Feb 29, 2024
1 parent b2ace28 commit 9cf4255
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Remember, the 75 Hard Challenge is about personal growth and discipline. Stay co
- Day 12: [2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)
- Day 12: [21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)
- Day 13: [138. Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)
- Day 14: [92. Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,27 @@
# self.next = next
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
pass

if not head or left == right:
return head

dummy = ListNode(0)
dummy.next = head
prev = dummy

for _ in range(left-1):
prev = prev.next

stack = []
current = prev.next

for _ in range(right-left+1):
stack.append(current)
current = current.next

while stack:
prev.next = stack.pop()
prev = prev.next
prev.next = current

return dummy.next

0 comments on commit 9cf4255

Please sign in to comment.