Skip to content

Linked List Copy

01 · Question

Given the head of a singly linked list, return a new list with the same values.

02 · Solution

Reference solution

1def copy_list(head: Optional[ListNode]) -> Optional[ListNode]:
2 dummy = ListNode(0)
3 tail = dummy
4 cur = head
5 while cur:
6 tail.next = ListNode(cur.val)
7 tail = tail.next
8 cur = cur.next
9 return dummy.next