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

来源:互联网 发布:ubuntu 查看mysql状态 编辑:程序博客网 时间:2024/06/05 22:41

题目描述:

删除链表中等于给定值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;}


原创粉丝点击