-
Notifications
You must be signed in to change notification settings - Fork 119
/
61.c
72 lines (56 loc) · 1.4 KB
/
61.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* rotateRight(struct ListNode* head, int k) {
if (head == NULL || k == 0) return head;
struct ListNode **p = &head;
struct ListNode *new_head = NULL;
int len = 0;
while (*p) {
p = &((*p)->next);
len++;
}
k = k % len; /* important */
if (k == 0) return head;
*p = head; /* tail->next = head, make a circle */
p = &head;
while (len > k) {
p = &((*p)->next);
len--;
}
new_head = *p; /* new_head = new_tail->next */
*p = NULL; /* new_tail = NULL */
return new_head;
}
int main() {
struct ListNode *l1 = (struct ListNode *)calloc(5, sizeof(struct ListNode));
struct ListNode **p = &l1;
int i;
for (i = 1; i <= 5; i++) {
(*p)->val = i;
(*p)->next = *p + 1;
p = &(*p)->next;
}
*p = NULL;
printf("List: ");
struct ListNode *q = l1;
while (q != NULL) {
printf("%d->", q->val);
q = q->next;
}
printf("N\n");
int k = 2;
printf("Rotate right by %d.\n", k);
struct ListNode *ret = rotateRight(l1, k);
printf("Result: ");
q = ret;
while (q != NULL) {
printf("%d->", q->val);
q = q->next;
}
printf("N\n");
return 0;
}