-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day 13: Attempt 2: 138_Copy_List_with_Random_Pointer
- Loading branch information
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
8 changes: 8 additions & 0 deletions
8
top_interview_questions/75_hard/Linked List/92_Reverse_Linked_List_II.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |