Skip to content

Commit

Permalink
Day 13: Attempt 2: 138_Copy_List_with_Random_Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
rjsnh1522 committed Feb 28, 2024
1 parent 61b0fce commit b2ace28
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LeetCode offers a valuable platform for coding practice and problem-solving, esp
## Stay Committed
Remember, the 75 Hard Challenge is about personal growth and discipline. Stay committed to your goals, and track your journey. You've got this!

## Progress ![Progress](https://img.shields.io/badge/47%2F150-28a745)
## Progress ![Progress](https://img.shields.io/badge/53%2F150-28a745)


*In first attempt I solved questions for 21 days*
Expand Down Expand Up @@ -53,6 +53,7 @@ Remember, the 75 Hard Challenge is about personal growth and discipline. Stay co
- Day 12: [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)
- 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/)



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random

class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
oldToCopy = {None:None}

cur = head
while cur:
copy = Node(cur.val)
oldToCopy[cur] = copy
cur = cur.next

cur = head
while cur:
copy = oldToCopy[cur]
copy.next = oldToCopy[cur.next]
copy.random = oldToCopy[cur.random]
cur = cur.next
return oldToCopy[head]





sol = Solution()
head = [[7,None],[13,0],[11,4],[10,2],[1,0]]
sol.copyRandomList(head=head)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
pass

0 comments on commit b2ace28

Please sign in to comment.