Mark As Completed Discussion

Example 2: Clone a Linked List

The ptr pointer is used only in the special case where it is null. The tail pointer is then used in the standard way to create copies after the special case is done.

The idea is to iterate over the original linked list and maintain 2 pointers, ptr and tail to keep track of the new linked list.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment