Skip to content

Commit

Permalink
Copy list with random pointer
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Nov 10, 2017
1 parent e6b82a9 commit bfc015f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 138_copy_list_with_random_pointer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test copy_list.c
83 changes: 83 additions & 0 deletions 138_copy_list_with_random_pointer/copy_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>

struct RandomListNode {
int label;
struct RandomListNode *next;
struct RandomListNode *random;
};

static struct RandomListNode *copyRandomList(struct RandomListNode *head)
{
if (head == NULL) {
return NULL;
}

struct RandomListNode *p, *new;
for (p = head; p != NULL; p = p->next->next) {
new = malloc(sizeof(*new));
new->label = p->label;
new->next = p->next;
p->next = new;
}

for (p = head; p != NULL; p = p->next->next) {
new = p->next;
new->random = p->random != NULL ? p->random->next : NULL;
}

struct RandomListNode dummy;
struct RandomListNode *prev = &dummy;
for (p = head; p != NULL; p = p->next) {
new = p->next;
p->next = new->next;
prev->next = new;
prev = new;
new->next = NULL;
}

return dummy.next;
}

int main(int argc, char **argv)
{
int i, count = argc - 1;
struct RandomListNode *head = NULL, *p, *prev;
for (i = 0; i < count; i++) {
p = malloc(sizeof(*p));
p->label = atoi(argv[i + 1]);
p->next = NULL;
p->random = NULL;
if (head == NULL) {
head = p;
} else {
prev->next = p;
prev->random = p;
}
prev = p;
}

struct RandomListNode *r = head;
struct RandomListNode *q = p = copyRandomList(head);

for (r = head; r != NULL; r = r->next) {
printf("%d ", r->label);
}
printf("\n");

for (r = head; r != NULL; r = r->random) {
printf("%d ", r->label);
}
printf("\n");

for (; p != NULL; p = p->next) {
printf("%d ", p->label);
}
printf("\n");

for (; q != NULL; q = q->random) {
printf("%d ", q->label);
}
printf("\n");
return 0;
}

0 comments on commit bfc015f

Please sign in to comment.