Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1028721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:26:06+00:00 2026-05-16T12:26:06+00:00

Possible Duplicate: Copy a linked list Hello stackoverflow! I am trying to learn more

  • 0

Possible Duplicate:
Copy a linked list

Hello stackoverflow! I am trying to learn more about linked lists so I am trying to create a function that deep copies a linked list. I’ve got this under control. The hard part is that the input list is going to contain nodes that reference other random nodes within the list.

First problem is that I don’t know how to create the ‘random’ nodes within the list. As of now, I just have the ‘random’ nodes equal to the ‘next’ nodes.

For example… pNext references the next value, but pReference will reference a random node in the list. Like pReference 1 references 3, 2 references 4, 3 references 1, and 4 references 1.

Second problem is that my code is coping the ‘random’ node values but it is dependent on the original copy. I want it to be a deep copy and not dependent on the original.

#include <iostream>
#include <stdio.h>

using namespace std;

struct Node
{
    int Number; // An integer value.
    Node *pNext; // A pointer to the next node within the list.
    Node *pReference; // A pointer to a random node within the list.
};

void push(Node** head, int data, Node* reference)
{
    Node* newNode = new Node; 
    newNode->Number = data;
    newNode->pNext = *head;
    newNode->pReference = reference;
    *head = newNode;
}

Node* DuplicateLinkedList(Node *head)
{
    Node* current = head;
    Node* newHead = NULL;
    Node* newTail = NULL;

    while(current != NULL)
    {
        if(newHead == NULL)
        {
            push(&newHead, current->Number, (current->pReference));
            newTail = newHead;
        }
        else
        {
            push(&(newTail->pNext),current->Number, (current->pReference));
            newTail = newTail->pNext;
        }
        current = current->pNext;
    }

    return newHead;
}

int main()
{
    Node* headOfList= NULL;

    //Creating List for verification.
    for(int i=6; i>=1;i--)
    {
        push(&headOfList, i, headOfList);
    }

    //Call duplicate function.
    Node* copiedList = DuplicateLinkedList(headOfList);

    //Output for verification
    cout << endl << "Original: " << endl;
    while(headOfList != NULL)
    {
        cout << "Number: " << headOfList->Number << " ";
        cout << "pNext: " << headOfList->pNext << " ";
        cout << "pReference: " << headOfList->pReference << " " << endl;
        headOfList = headOfList->pNext;
    }
    cout << endl << endl;

    cout << endl << "Copied: " << endl;
    while(copiedList != NULL)
    {
        cout << "Number: " << copiedList->Number << " ";
        cout << "pNext: "  << copiedList->pNext << " ";
        cout << "pReference: " << copiedList->pReference << " " << endl;
        copiedList = copiedList->pNext;
    }
    cout << endl << endl;


    system("pause");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:26:07+00:00Added an answer on May 16, 2026 at 12:26 pm

    Use a std::map to store the conversion between the original and the new pointers.

    Walk the list two times: one time to create the new nodes (with pReference set to NULL) and to populate the map, a second time to fill in the pReference member by looking them up in the map.

    Untested code:

    Node* CopyNode(Node* src)
    {
      if (src == NULL) return NULL;
    
      Node* newNode = new Node;
      newNode->number = src->number;
      newNode->pNext = NULL;
      newNode->pReference = NULL;
    
      return newNode;
    }
    
    Node* DeepCopy(Node* head)
    {
      if (head == NULL) return NULL;
    
      std::map<Node*, Node*> mappings;
    
      Node* newHead = copyNode(head);
      mappings[head] = newHead;
    
      Node* newCurrent = newHead;
      for (Node* next = head->pNext; next != NULL; next = next->pNext)
      {
        Node* copy = CopyNode(next);
        mappings[next] = copy;
    
        newCurrent->pNext = copy;
        newCurrent = copy;
      }
    
      for (Node* current = head; current != NULL; current = current->pNext)
      {
        Node* newCurrent = mappings[current];
        newCurrent->pReference = mappings[current->pReference];
      }
    
      return newHead;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 529k
  • Answers 529k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer @ can be used to "escape" identifiers, in case you… May 16, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer Looking at http://www.boost.org/doc/libs/1_44_0/boost/type_traits/is_enum.hpp, If this evaluates to true: ::boost::type_traits::ice_or< ::boost::is_arithmetic<T>::value… May 16, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer try to ping it or connect over TCP/IP to some… May 16, 2026 at 11:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Possible Duplicate: question about copy constructor if I have this snippet of the code
Possible Duplicate: Copy to Output Directory copies folder structure but only want to copy
Possible Duplicate: How to copy char *str to char c[] in C? char *token
Possible Duplicate: How disable Copy, Cut, Select, Select All in UITextView I have some
Possible Duplicate: How to copy a file to another path? hi, how to copy
Possible Duplicate: How to copy part of an array to another array in C#?
Possible Duplicate: how to copy char * into a string and vice-versa I have
Possible Duplicate: Best way to copy the entire contents of a directory in C#
Possible Duplicate: How to copy part of an array to another array in C#
Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.