Skip to content

Commit 72fc910

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent f583b44 commit 72fc910

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

138_copy_list_with_random_pointer/copy_list.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
struct RandomListNode {
5-
int label;
6-
struct RandomListNode *next;
7-
struct RandomListNode *random;
4+
struct Node {
5+
int val;
6+
struct Node *next;
7+
struct Node *random;
88
};
99

10-
static struct RandomListNode *copyRandomList(struct RandomListNode *head)
10+
static struct Node *copyRandomList(struct Node *head)
1111
{
1212
if (head == NULL) {
1313
return NULL;
1414
}
1515

16-
struct RandomListNode *p, *new;
16+
/* copy and redirect next pointer */
17+
struct Node *p, *new;
1718
for (p = head; p != NULL; p = p->next->next) {
1819
new = malloc(sizeof(*new));
19-
new->label = p->label;
20+
new->val = p->val;
2021
new->next = p->next;
2122
p->next = new;
2223
}
2324

25+
/* clone random pointer */
2426
for (p = head; p != NULL; p = p->next->next) {
2527
new = p->next;
2628
new->random = p->random != NULL ? p->random->next : NULL;
2729
}
2830

29-
struct RandomListNode dummy;
30-
struct RandomListNode *prev = &dummy;
31+
struct Node dummy;
32+
struct Node *prev = &dummy;
3133
for (p = head; p != NULL; p = p->next) {
3234
new = p->next;
3335
p->next = new->next;
36+
/* correct the actual next pointer of the new list */
3437
prev->next = new;
3538
prev = new;
3639
new->next = NULL;
@@ -42,10 +45,10 @@ static struct RandomListNode *copyRandomList(struct RandomListNode *head)
4245
int main(int argc, char **argv)
4346
{
4447
int i, count = argc - 1;
45-
struct RandomListNode *head = NULL, *p, *prev;
48+
struct Node *head = NULL, *p, *prev;
4649
for (i = 0; i < count; i++) {
4750
p = malloc(sizeof(*p));
48-
p->label = atoi(argv[i + 1]);
51+
p->val = atoi(argv[i + 1]);
4952
p->next = NULL;
5053
p->random = NULL;
5154
if (head == NULL) {
@@ -57,26 +60,26 @@ int main(int argc, char **argv)
5760
prev = p;
5861
}
5962

60-
struct RandomListNode *r = head;
61-
struct RandomListNode *q = p = copyRandomList(head);
63+
struct Node *r = head;
64+
struct Node *q = p = copyRandomList(head);
6265

6366
for (r = head; r != NULL; r = r->next) {
64-
printf("%d ", r->label);
67+
printf("%d ", r->val);
6568
}
6669
printf("\n");
6770

6871
for (r = head; r != NULL; r = r->random) {
69-
printf("%d ", r->label);
72+
printf("%d ", r->val);
7073
}
7174
printf("\n");
7275

7376
for (; p != NULL; p = p->next) {
74-
printf("%d ", p->label);
77+
printf("%d ", p->val);
7578
}
7679
printf("\n");
7780

7881
for (; q != NULL; q = q->random) {
79-
printf("%d ", q->label);
82+
printf("%d ", q->val);
8083
}
8184
printf("\n");
8285
return 0;

143_reorder_list/reorder_list.c

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,48 @@ struct ListNode {
66
struct ListNode *next;
77
};
88

9-
static void reverse(struct ListNode *dummy)
10-
{
11-
struct ListNode *prev = dummy->next;
12-
if (prev != NULL) {
13-
struct ListNode *p = prev->next;
14-
while (p != NULL) {
15-
prev->next = p->next;
16-
p->next = dummy->next;
17-
dummy->next = p;
18-
p = prev->next;
19-
}
20-
}
21-
}
22-
239
static void reorderList(struct ListNode *head)
2410
{
25-
if (head == NULL) {
11+
if (head == NULL || head->next == NULL) {
2612
return;
2713
}
2814

2915
int count = 0;
30-
struct ListNode *p = head;
31-
struct ListNode *q = p;
16+
struct ListNode dummy;
17+
struct ListNode *prev = &dummy;
18+
struct ListNode *p, *q;
3219

3320
/* locate half length */
34-
for (; p != NULL; p = p->next) {
21+
dummy.next = head;
22+
for (p = head, q = head; q != NULL; q = q->next) {
3523
if ((++count & 0x1) == 0) {
36-
q = q->next;
24+
prev = p;
25+
p = p->next;
3726
}
3827
}
3928

4029
/* reverse latter half list */
41-
reverse(q);
30+
while (p->next != NULL) {
31+
q = p->next;
32+
/* deletion */
33+
p->next = q->next;
34+
/* insertion */
35+
q->next = prev->next;
36+
prev->next = q;
37+
}
4238

43-
/* insert each element */
44-
struct ListNode *r;
45-
for (p = head, r = q->next; r != NULL; p = r->next, r = q->next) {
46-
q->next = r->next;
47-
r->next = p->next;
48-
p->next = r;
49-
}
39+
/* insert each element interleavingly */
40+
struct ListNode *last = prev;
41+
p = head;
42+
while (p != last) {
43+
q = last->next;
44+
/* deletion */
45+
last->next = q->next;
46+
/* insertion */
47+
q->next = p->next;
48+
p->next = q;
49+
p = q->next;
50+
}
5051
}
5152

5253
int main(int argc, char **argv)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy