/*
仅用一次循环删除单链表倒数第k个节点 并做健壮性判断
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct _Node{int v;struct _Node *nxt;} Node,*pNode,*List;
_Bool deleteKth_From_EndPoint(List list,int k){
if(!list || !(k > 0))return 0;
pNode fast = list,
slow = list,
prev = NULL;
while(k -- && fast)fast = fast->nxt;
if(k)return 0;
while(fast){
fast = fast->nxt;
slow = ( prev = slow )->nxt;
}
if(prev){
prev->nxt = slow->nxt;free(slow);
return 1;
}
return 0;
}
快慢双指针删除链表倒数第K个节点
最新推荐文章于 2024-08-22 11:14:08 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)