|
1 | 1 | /* |
2 | 2 | * Task: Deep copy a linked list with sibling pointers. |
3 | 3 | * |
4 | | - * DEEP COPY OF A COMPLEX LINKED LIST WITH SIBLING POINTERS |
| 4 | + * COPY LIST WITH RANDOM (SIBLING) POINTER |
5 | 5 | * |
6 | | - * Define a class named ComplexList that represents a singly linked list where |
7 | | - * each node has an integer value, a pointer to the next node, and an additional |
8 | | - * "sibling" pointer that can reference any other node in the list. The class |
9 | | - * supports operations for appending nodes, setting sibling pointers, and |
10 | | - * performing a deep copy of the entire list while preserving the sibling |
11 | | - * relationships. |
| 6 | + * Problem: |
| 7 | + * Given a linked list where each node has `next` and `sibling` pointers, |
| 8 | + * create a deep copy of the list. The copied nodes must be new objects, and |
| 9 | + * every copied sibling pointer must reference the corresponding copied node. |
12 | 10 | * |
13 | | - * ASCII Illustration: |
| 11 | + * Constraints: |
| 12 | + * - 0 <= n <= 10^5 |
| 13 | + * - Node values are 32-bit signed integers. |
| 14 | + * - `sibling` is either null or points to any node in the same list. |
14 | 15 | * |
15 | | - * Original List: Copied List: |
| 16 | + * Example 1: |
| 17 | + * Input: values = [1,2,3,4,5], sibling = {1->3, 2->5, 5->2} |
| 18 | + * Output: deep-copied list with identical `next` and `sibling` structure |
16 | 19 | * |
17 | | - * 1 --> 2 --> 3 1 --> 2 --> 3 |
18 | | - * \ \ \ \ |
19 | | - * v v v v |
20 | | - * 3 5 3 5 |
21 | | - * |
22 | | - * Example: |
23 | | - * Input List: |
24 | | - * Node values: 1 -> 2 -> 3 -> 4 -> 5 |
25 | | - * Sibling relationships: |
26 | | - * - 1's sibling -> 3 |
27 | | - * - 2's sibling -> 5 |
28 | | - * - 5's sibling -> 2 |
29 | | - * |
30 | | - * When copied, both lists will have the same structure and sibling connections. |
| 20 | + * Example 2: |
| 21 | + * Input: values = [1], sibling = {} |
| 22 | + * Output: [1] (copied node, not the same address) |
31 | 23 | */ |
32 | 24 |
|
33 | 25 | #include "list.h" |
|
0 commit comments