• LeetCode: Remove Linked List Elements


    Remove all elements from a linked list of integers that have value val.

    Example
    Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
    Return: 1 –> 2 –> 3 –> 4 –> 5

    解析:

    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            ListNode* newhead = new ListNode(-1);
            newhead->next = head;
            ListNode* pre = newhead;
            ListNode* cur = head;
            while(cur != NULL)
            {
                if(cur->val == val)
                {
                    cur = cur->next;
                    pre->next = cur;
                }
                else
                {
                    pre = cur;
                    cur = cur->next;
                }
            }
            return newhead->next;
        }
    };

    方法二:

    void removeHelper(ListNode *&head, int val)
    {
        if (head == nullptr)
        {
            return;
        }
        else if (head->val == val)
        {
            head = head->next;
        }
        else
        {
            removeHelper(head->next, val);
        }
    }

    方法三(有问题一直不能AC):

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if (!head)
                return NULL;
            if (head->next == NULL)
                return head;
            ListNode *p = head->next;
            ListNode *pre = head;
            while (p)
            {
                if (p->val == val)
                {
                    ListNode *r = p->next;
                    pre->next = r;
                    free(p);
                    p = r;
                }
                else
                {
                    pre = p;
                    p = p->next;
                }
    
            }
            return head;
        }
    };
    

    方法四:

    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            ListNode *p = head;
            ListNode *pre = head;
            while (p)
            {
                if (p->val == val)
                {
                    if(p==head)
                        pre = head = p->next;
                    else
                        pre->next = p->next;
    
                    free(p);
                    p = pre;
                }
                else
                {
                    pre = p;
                    p = p->next;
                }
    
            }
            return head;
        }
    };
  • 相关阅读:
    EF中嵌套类的where查询
    VS中添加Web References
    DropDownList绑定数据源后,要插入项的处理
    CheckBoxList选中某项,获取其它项是否是选中
    WebAPI的使用
    HTML5中像网页中保存cookie的实现
    日志切割之Logrotate
    CentOS防火墙iptables使用
    CentOS7安装Python3
    Keepalived高可用
  • 原文地址:https://www.cnblogs.com/chankeh/p/6850061.html
Copyright © 2020-2023  润新知