forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.c
73 lines (73 loc) · 1.29 KB
/
solve.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
73
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *removeNthFromEnd(struct ListNode *head, int n) {
if (head == NULL)
return NULL;
struct ListNode *p = head, *q = head;
int i = 0;
for (i = 0; i < n && p; ++i) p = p -> next;
struct ListNode *t = NULL;
if (i < n)
return head;
while (p) {
p = p -> next;
t = q;
q = q -> next;
}
if (t != NULL) {
t -> next = q -> next;
q -> next = NULL;
free(q);
q = NULL;
return head;
} else {
head = head -> next;
free(q);
q = NULL;
return head;
}
}
void init(struct ListNode **head, int a[], int n)
{
struct ListNode *p = malloc(sizeof(*p));
p -> val = a[0];
*head = p;
for (int i = 1; i < n; ++i) {
struct ListNode *t = malloc(sizeof(*t));
t->val = a[i];
p -> next = t;
p = t;
}
}
void print(struct ListNode *head)
{
struct ListNode *p = head;
while (p) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
struct ListNode *rm(struct ListNode *head, int n)
{
return removeNthFromEnd(head, n);
}
int main(int argc, char **argv)
{
struct ListNode *head = NULL;
int a[] = {1,2,3,4,5};
init(&head, a, 5);
print(head);
head = rm(head, 2);
print(head);
head = rm(head, 4);
print(head);
head = rm(head, 100);
print(head);
return 0;
}