139 在链表中删除指定值的节点
Table of Contents

ac

# include <bits/stdc++.h>
using namespace std;

struct list_node{
    int val;
    struct list_node * next;
};

list_node * input_list()
{
    int val, n;
    scanf("%d", &n);
    list_node * phead = new list_node();
    list_node * cur_pnode = phead;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &val);
        if (i == 1) {
            cur_pnode->val = val;
            cur_pnode->next = NULL;
        }
        else {
            list_node * new_pnode = new list_node();
            new_pnode->val = val;
            new_pnode->next = NULL;
            cur_pnode->next = new_pnode;
            cur_pnode = new_pnode;
        }
    }
    return phead;
}


list_node * remove_value(list_node * head, int num)
{
    //////在下面完成代码
    if(head == NULL){
        return head;
    }
    list_node* pHead = head;
    while(pHead != NULL && pHead->val == num){
        pHead = pHead->next;
    }
    if(pHead == NULL){
        return pHead;
    }
    list_node* pre = pHead;
    list_node* p = pHead->next;
    list_node* pTail = pHead;
    pTail->next = NULL;
    while(p != NULL){
        list_node* pNext = p->next;
        if(p->val != num){
            pTail->next = p;
            pTail = p;
            pTail->next = NULL;
        }
        p = pNext;
    }
    return pHead;
}

void print_list(list_node * head)
{
    while (head != NULL) {
        printf("%d ", head->val);
        head = head->next;
    }
    puts("");
}

int main ()
{
    list_node * head = input_list();
    int num;
    scanf("%d", &num);
    list_node * new_head = remove_value(head, num);
    print_list(new_head);
    return 0;
}
/**
4
1 2 3 4
3

1 2 4
*/