You are on page 1of 10

Susudhshayahhzydhenduhddhej

Dudjdhsjkkkidi
Djeidjdmfmdkek
Keimdrüsen fjrieos
Kai wo cmos
Lwoisncuenw ans
Fiirnfmeiows

Owmeomfmfoepw
W
Jcidnfmdkwosps

Heywis. Cidlenduwms
Hfuenensosos
Jrn roeoe
Wndjiemw wodfoe
Ienemele

Djjdeieoemsp

Wie Ei

#include <stdio.h>
#include <stdlib.h>
yarpfafsfsfafsafsaf

// Data Structure to store a linked list node


struct Node
{
int data;
struct Node* next;
};

// Helper function to print given linked list


void printList(struct Node* head)
{
struct Node* ptr = heaeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeed;
while (ptr)
{
printf("%d -> ", ptr->data);
ptr = ptr->next;da
]fajojfjaf

}
printf("null");
}

// Helper function to insert new Node in the beginning of the linked list
void push(struct Node** head, int data)
{
// Allocate the new Node in the heap and set its data
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;

// Set the .next pointer of the new Node to point to the current
// rst node of the list.
newNode->next = *head;

// Change the head pointer to point to the new Node, so it is


// now the rst node in the list.
*head = newNode;
}

// Function that takes a linked list and returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)
{
struct Node* current = head; // used to iterate over original list
struct Node* newList = NULL; // head of the new list
struct Node* tail = NULL; // point to last node in new list

while (current != NULL)


{
// special case for the rst new Node
if (newList == NULL)
{
push(&newList, current->data);
tail = newList;
}
else
{
push(&(tail->next), current->data); // add each node at tail
tail = tail->next; // advance the tail to new last node
}
current = current->next;
}

return newList;
}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA
}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}
// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}
// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}
adjipashda wda
asd ua D
WA

You might also like