deep copy of a linked list

Post on 22-Jan-2018

192 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

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: …

top related