欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

删除倒数第n个链表的值

程序员文章站 2022-06-24 21:29:57
...

遍历法,
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;

}

相关标签: IT技术