deep copy of a linked list
TRANSCRIPT
ListNode
data: l
next: l
Suppose we want to deep copy a list node, but not the ReadThis object…
ReadThis
url: l
currentPoints: 0
String
value: …
ListNode
data: l
next: l
ReadThis
url: l
currentPoints: 0
String
value: …
Make a new ListNode:
ListNode
data: l
next: l
ListNode
data: l
next: l
Make a new ReadThis:
ListNode
data: l
next: l
name: l
currentPoints: 0
ReadThis
ReadThis
url: l
currentPoints: 0
String
value: …
name: l
currentPoints: 0
ReadThis
ListNode
data: l
next: l
Copy the ReadThis’s String reference and currentPoints value:
ListNode
data: l
next: l
ReadThis
url: l
currentPoints: 0
String
value: …
ListNode
data: l
next: l
Point the new list node’s data to the new ReadThis:
ListNode
data: l
next: l
name: l
currentPoints: 0
ReadThis
ReadThis
url: l
currentPoints: 0
String
value: …
ListNode
data: l
next: l
Now we have a deep copy of the ListNode.
ListNode
data: l
next: l
name: l
currentPoints: 0
ReadThis
ReadThis
url: l
currentPoints: 0
String
value: …