#include<bits/stdc++.h>
using namespace std;
struct node {
node *next;
int val;
node() {
next = NULL;}
};
int main() {
int n, k, v;
scanf("%d %d", &n, &k);
node *head = new node(), *p = head;
for (int i = 1; i <= n; i++) {
scanf("%d", &v);
p->val = v;
if (i != n) {
p->next = new node;
p = p->next;
}
}
p = head;
k = n - k + 1;
if (k == 1) {
node *q = head;
head = head->next;
delete q;
}
p = head;
int cnt = 1;
while (p) {
printf("%d ", p->val);
if (cnt == k - 1) {
node *q = p->next;
p->next = q->next;
delete q;
}
cnt++;
p = p->next;
}
// Free memory
while (head) {
node *q = head;
head = head->next;
delete q;
}
return 0;
}
1267:删除顺序表倒数第k个元素
最新推荐文章于 2024-08-15 16:09:23 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)