Skip to content

Commit e1b0fc6

Browse files
author
lightmen
committed
modify copy-list-with-random-pointer.c
1 parent 8c27b66 commit e1b0fc6

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

copy-list-with-random-pointer.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
*/
99
struct RandomListNode *copyRandomList(struct RandomListNode *head) {
1010
struct RandomListNode root;
11-
struct RandomListNode *prev,*tmp,*p,*tmp_head;
11+
struct RandomListNode *prev,*tmp,*p;
1212
struct RandomListNode **arr;
1313
int count = 0;
1414
int i;
15+
1516
if(!head)
1617
return NULL;
1718

18-
root.next = NULL;
1919
prev = &root;
2020
while(head){
2121
tmp = (struct RandomListNode *)malloc(sizeof(struct RandomListNode));
@@ -35,6 +35,7 @@ struct RandomListNode *copyRandomList(struct RandomListNode *head) {
3535
arr = (struct RandomListNode **)malloc(sizeof(struct RandomListNode *) * count);
3636
i = 0;
3737
head = root.next;
38+
3839
while(head){
3940
p = head->random;
4041
tmp = p->random;
@@ -45,7 +46,10 @@ struct RandomListNode *copyRandomList(struct RandomListNode *head) {
4546

4647
for(i = 0; i < count-1; ++i)
4748
arr[i]->next = arr[i+1];
49+
4850
arr[count-1]->next = NULL;
4951
head = arr[0];
52+
free(arr);
53+
5054
return root.next;
5155
}

0 commit comments

Comments
 (0)