删除倒数第n个链表的值

强颜欢笑 提交于 2020-02-06 08:56:53

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

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!