zoukankan      html  css  js  c++  java
  • LeetCode 203. 移除链表元素

    题目描述:

    解法一(自然想法):

    /**
     * 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) {
            ListNode* pre = NULL, *now = head, *res = head;
            while (now != NULL) {
                if (now->val == val) {          
                    if (now == res) {                //过滤头节点
                        res = now->next;
                        now = res;
    
                    }
                    else {                            //过滤平常结点
                        pre->next = now->next;
                        now = now->next;
                    }
                }
                else {
                    pre = now;
                    now = now->next;
                }
            }
            return res;
        }
    };

    解法二(删除头结点时另做考虑(由于头结点没有前一个结点)):

    /**
     * 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) {
            while(head!=NULL&&head->val==val) head=head->next;        //滤除头节点
            if(head==NULL) return head;
            ListNode* now=head;
            while(now->next!=NULL){
                if(now->next->val==val){                              //滤除普通节点
                    now->next=now->next->next;
                }
                else
                    now=now->next;
            }
            return head;
        }
    };

    解法三(添加一个虚拟头结点,删除头结点就不用另做考虑 ):

    /**
     * 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) {
            ListNode* nHead=new ListNode(val-1);                //添加虚拟头结点
            nHead->next=head;
            ListNode* now=nHead;
            while(now->next!=NULL){
                if(now->next->val==val){
                    now->next=now->next->next;
                }
                else
                    now=now->next;
            }
            ListNode* res=nHead->next;
            delete nHead;
            return res;
        }
    };

    解法四(递归):

    /**
     * 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==NULL) return NULL;
            head->next=removeElements(head->next,val);
            if(head->val==val){
                return head->next;
            }
            else return head;
        }
    };
  • 相关阅读:
    iOS删除Scene
    IOS如何解决强引用中的循环引用
    IOS中performSelector线程使用
    IOS中的类方法和实例方法
    Mac系统上安装Java的JDK并配置环境变量
    Java开发基本环境知识
    SVN上传工具Cornerstone的简单使用
    05--学习PHP面向对象--静态成员与常量成员
    04--学习PHP面向对象--访问权限修饰符
    03--学习PHP面向对象--$this
  • 原文地址:https://www.cnblogs.com/oneDongHua/p/14264015.html
Copyright © 2011-2022 走看看