遍历法,
struct Node {
int val;
struct Node *next;
};
void deleteNode(struct Node *temp, int n) {
struct Node *start = temp;
int len = 1;
if (temp == null)
return;
while (start != null) {
start = start->next;
len++;
}
if (len < n) {
printf("error");
return;
}
start = temp;
struct Node *temp1 = start;
if (len == n) {
start = start->next;
delete(start);
}
for (int i = 0; i < (len - n); i++) {
temp1 = start;
start = start->next;
}
temp1->next = start->next;
delete(start);
return;
}