Copy Linked List - codepath/compsci_guides GitHub Wiki

Unit 5 Session 2 (Click for link to problem statements)

TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 15-20 mins
  • 🛠️ Topics: Linked Lists, Deep Copy

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What happens if the original linked list is empty?
    • The function should return None as the head of the new linked list.
  • Does the new linked list share any nodes with the original linked list?
    • No, the new linked list should be a completely independent copy.
HAPPY CASE
Input: head = Node("Mario") -> Node("Daisy") -> Node("Luigi")
Output: head of new linked list with same values and structure but different nodes.
Explanation: A new linked list is created with the same values and structure as the original, but it is a deep copy.

EDGE CASE
Input: head = None
Output: None
Explanation: When the original linked list is empty, the function returns None.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Linked List problems, we want to consider the following approaches:

  • Deep copying of linked lists
  • Iterative traversal to replicate node values and structure

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the original linked list, creating a new node for each node in the original list, and link these nodes together to form a new linked list.

1) Create a mock node to serve as the starting point for the new linked list.
2) Initialize a pointer `current_new` to the mock node and a pointer `current_original` to the head of the original linked list.
3) Traverse the original linked list:
    a) Create a new node with the same value as the current original node.
    b) Append this new node to the end of the new linked list.
    c) Move both the new list pointer and the original list pointer to their respective next nodes.
4) Return the `next` of the mock node, which is the head of the newly copied linked list.

⚠️ Common Mistakes

  • Forgetting to return the head of the new linked list.
  • Accidentally sharing nodes between the original and copied linked lists, leading to unintended side effects.

4: I-mplement

Implement the code to solve the algorithm.

def copy_ll(head):
    # Mock node to start the new linked list
    mock_head = Node(0)
    current_new = mock_head
    
    # Current pointer for the original linked list
    current_original = head
    
    # Traverse the original linked list
    while current_original:
        # Create a new node with the same value as the original node
        new_node = Node(current_original.value)
        # Append it to the end of the new linked list
        current_new.next = new_node
        # Move the new list pointer forward
        current_new = current_new.next
        # Move to the next node in the original linked list
        current_original = current_original.next
    
    # The head of the new singly linked list
    return mock_head.next

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Verify that the new linked list has the same values and structure as the original but does not share any nodes with it.

Example:

head = Node("Mario")
head.next = Node("Daisy")
head.next.next = Node("Luigi")

# Copy the linked list
copy = copy_ll(head)

# Modify the original linked list to ensure the copy remains unchanged
head.value = "Peach"
print_linked_list(head)  # Expected Output: "Peach -> Daisy -> Luigi"
print_linked_list(copy)  # Expected Output: "Mario -> Daisy -> Luigi"

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(N) because we need to traverse all nodes in the linked list to copy them.
  • Space Complexity: O(N) for the space required to store the new nodes in the copied linked list.