0
点赞
收藏
分享

微信扫一扫

LintCode 第452题 删除链表中的元素


题目描述:

删除链表中等于给定值​​val​​的所有节点。

样例


给出链表 ​​1->2->3->3->4->5->3​​, 和 val = ​​3​​, 你需要返回删除3之后的链表:​​1->2->4->5​​。 代码实现:


#include <iostream>
//链表结点结构
struct Node {
int value;
struct Node * next;
};

//创建单链表
struct Node * creatLink(int n) {
struct Node * pHead = NULL;
for (int i = n; i>0; i--) {
struct Node * p = (struct Node *)malloc(sizeof(struct Node));
p->value = i;
p->next = pHead;
pHead = p;
}
return pHead;
}

//移除指定节点
void removeLink(struct Node* pHead,int removeValue) {
if (pHead->next->value != removeValue) {
removeLink(pHead->next,removeValue);
} else {
pHead->next = pHead->next->next;
}
}

int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";

struct Node * pHead = creatLink(5);
removeLink(pHead,3);
return 0;
}



举报

相关推荐

0 条评论