删除倒数第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;
}
推荐阅读
-
剑指offer25:复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),结果返回复制后复杂链表的head。
-
python实现单链表中删除倒数第K个节点的方法
-
python字典值排序并取出前n个key值的方法
-
PHP获取链表中倒数第K个节点的方法
-
LeetCode 19. 删除链表的倒数第N个节点
-
#leetcode刷题之路19-删除链表的倒数第N个节点
-
LeetCode 19. 删除链表的倒数第N个节点(双指针和递归的妙用)
-
php 删除一个数组中的某个值.兼容多维数组!
-
php 删除一维数组中某一个值元素的操作方法
-
链表中倒数第k个节点最简单的实现方式--python