0% found this document useful (0 votes)
2 views

Algorithm to Copy a Singly Linked List

The document outlines an algorithm to create an identical copy of a singly linked list, detailing steps to check for an empty list, create new nodes, and link them. It includes C code for functions to create nodes, insert them at the end, copy the linked list, and print the list. The complexity analysis indicates that both the time and space complexity for copying the list is O(n).

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm to Copy a Singly Linked List

The document outlines an algorithm to create an identical copy of a singly linked list, detailing steps to check for an empty list, create new nodes, and link them. It includes C code for functions to create nodes, insert them at the end, copy the linked list, and print the list. The complexity analysis indicates that both the time and space complexity for copying the list is O(n).

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithm to Copy a Singly Linked List

Given a singly linked list, we need to create an identical copy.

Algorithm:

1. Check if the list is empty: If head == NULL, return NULL.

2. Create a new head node for the copy using malloc().

3. Initialize a pointer (current) to traverse the original list.

4. Use a tail pointer to build the new list.

5. Iterate through the original list:

o Create a new node for each existing node.

o Copy its data and link it to the new list.

o Move tail forward.

6. Return the new list’s head.

C Code for Copying a Linked List

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int data;

struct Node* next;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;
return newNode;

// Function to insert a node at the end of the list

void insertAtEnd(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

return;

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

// Function to copy a linked list

struct Node* copyLinkedList(struct Node* head) {

if (head == NULL) return NULL; // If original list is empty, return NULL

struct Node* newHead = createNode(head->data); // Create the first node

struct Node* originalCurrent = head->next; // Traverse the original list

struct Node* copyTail = newHead; // Keep track of the last node in the copied list

// Traverse and copy each node

while (originalCurrent != NULL) {


copyTail->next = createNode(originalCurrent->data);

copyTail = copyTail->next;

originalCurrent = originalCurrent->next;

return newHead; // Return the head of the copied list

// Function to print a linked list

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

// Main function

int main() {

struct Node* originalList = NULL;

// Insert nodes into the original list

insertAtEnd(&originalList, 10);

insertAtEnd(&originalList, 20);

insertAtEnd(&originalList, 30);

insertAtEnd(&originalList, 40);

printf("Original List: ");


printList(originalList);

// Copy the linked list

struct Node* copiedList = copyLinkedList(originalList);

printf("Copied List: ");

printList(copiedList);

return 0;

Explanation

1. createNode

o Allocates memory for a new node and initializes it.

2. insertAtEnd

o Inserts nodes at the end of the linked list.

3. copyLinkedList

o If the list is empty, return NULL.

o Create a new head node for the copied list.

o Use originalCurrent to traverse the original list.

o Use copyTail to track the last node in the copied list.

o Copy each node’s data and link them together.

4. printList

o Prints the linked list.

5. Main Function

o Creates an original linked list.

o Copies it to a new list.

o Prints both lists.

Complexity Analysis
• Copying each node: O(n)

• Overall time complexity: O(n)

• Space complexity: O(n) (for storing copied nodes)

You might also like