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

(1)删除链表中等于给定值val的所有节点
给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。
(2)删除链表中重复的结点
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
(3)从尾到头打印链表

//笔试题:
/*
删除链表中等于给定值val的所有节点。
样例:
给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。
*/
struct Node{
    int value;
    Node* next;
};

Node* CreatNode(int value, Node* next = NULL){
    Node* node = new Node;
    node->value = value;
    node->next = next;
    return node;
}
Node* Delete(Node* head, int x){
    if (head == NULL){
        return head;
    }

    Node* prev = head;
    Node* cur = head->next;

    while (cur != NULL){
        if (x == cur->value){
            prev->next = cur->next;
            cur = cur->next;
        }
        else{
            prev = cur;
            cur = cur->next;
        }
    }

    if (x == head->value){
        head = head->next;
        }
    return head;
}

//打印链表
void PrintList(Node* head){
    Node* curr = head;
    while (curr != NULL){
        cout << curr->value << ends;
        curr = curr->next;
    }
    cout << endl;
}

//面试题5:从尾到头打印链表
void PrintListFromTail(Node* head){
    if (head == NULL){
        return;
    }
    else{
        PrintListFromTail(head->next);
        cout << head->value << ends;
    }
}

测试

void test(){
    Node* g = CreatNode(5);
    Node* f = CreatNode(4,g);
    Node* e = CreatNode(4,f);
    Node* d = CreatNode(3,e);
    Node* c = CreatNode(3,d);
    Node* b = CreatNode(2,c);
    Node* a = CreatNode(1,b);

    PrintList(a);

    Node* head=Delete(a, 3);
    PrintList(head);
    cout << endl;
    PrintListFromTail(head);

}
//删除已排序链表中重复的结点,重复节点不保留。
Node* DeleteDuplication(Node* head){
    if (head == NULL){
        return head;
    }

    Node* first = new Node;  //新建一个头结点就简单很多了
    first->next = head;

    Node* last = first;
    Node* curr = head;
    while (curr != NULL & curr->next != NULL){
        if (curr->value == curr->next->value){
            int v = curr->value;
            while (v == curr->value){
                curr = curr->next;
            }
            last->next = curr;
        }
        else{
            last = curr;
            curr = curr->next;
        }
    }
    return first->next;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言写的删除链表等于给定 val 的所有节点的代码,并附有注释: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct ListNode { int val; struct ListNode *next; }; // 定义删除链表等于给定 val 的所有节点的函数 struct ListNode* removeElements(struct ListNode* head, int val) { // 如果链表为空,直接返回 if (head == NULL) { return NULL; } // 如果链表节点等于 val,需要特殊处理 while (head != NULL && head->val == val) { head = head->next; } // 定义两个指针,分别指向当前节点和前一个节点 struct ListNode *curr = head; struct ListNode *prev = NULL; // 遍历链表删除等于 val节点 while (curr != NULL) { if (curr->val == val) { // 如果当前节点等于 val删除当前节点 if (prev != NULL) { prev->next = curr->next; } free(curr); curr = prev->next; } else { // 如果当前节点等于 val,继续遍历 prev = curr; curr = curr->next; } } return head; } int main() { // 创建链表 struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)); head->val = 1; head->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->val = 2; head->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->next->val = 3; head->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->next->next->val = 2; head->next->next->next->next = NULL; // 删除链表等于给定 val 的所有节点 int val = 2; head = removeElements(head, val); // 输出删除后的链表 struct ListNode *curr = head; while (curr != NULL) { printf("%d ", curr->val); curr = curr->next; } printf("\n"); return 0; } ``` 注:这段代码使用了链表的基本操作,包括创建链表、遍历链表删除节点等。其删除链表等于给定 val 的所有节点的函数 removeElements 的实现思路是:先处理头节点,再遍历链表删除等于 val节点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值