Copy Linked List with Random Pointer in O(n) Time

Some linked list problems go beyond simple pointers — they test how you handle multiple relationships between nodes. 🚀 Day 117/365 — DSA Challenge Solved: Copy List with Random Pointer Problem idea: We need to create a deep copy of a linked list where each node has an additional random pointer. Efficient approach: Use a HashMap to map original nodes to copied nodes. Steps: 1. Traverse the list and create a copy of each node 2. Store mapping: original → copy 3. Traverse again to assign next and random pointers 4. Return the copied head This ensures all pointers in the new list correctly reference copied nodes. ⏱ Time: O(n) 📦 Space: O(n) Day 117/365 complete. 💻 248 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LinkedList #HashMap #LeetCode #LearningInPublic

  • text

To view or add a comment, sign in

Explore content categories