• 203. Remove Linked List Elements *


    • Total Accepted: 99807
    • Total Submissions: 319528
    • Difficulty: Easy
    • Contributors: Admin

    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

    分析


    方法一 迭代 recursion

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    /**
     * 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 || head->next == NULL && head->val != val) return head;
             
            if(head->val == val){
                return removeElements(head->next, val);
            }
            else{
                head->next = removeElements(head->next, val);
                return head;
            }
        }
    };

    方法二 

    使用双指针,移相删除
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    /**
     * 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 || head->next == NULL && head->val != val) return head;
             
            ListNode dummy(-val);
            ListNode *pre = &dummy, * cur = head;
            dummy.next = head;
            while(cur){
                if(cur->val == val){
                    pre->next = cur->next;
                    cur = cur->next;
                }
                else{
                    pre = cur;
                    cur = cur->next;
                }
            }
            return dummy.next;
        }
    };

    方法三

    使用指向指针的指针
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if(head == NULL || head->next == NULL && head->val != val) return head;
             
            //consider head is a Node's next
            ListNode ** p = &head;
             
            while(*p){
                if((*p)->val == val){
                    (*p) = (*p)->next;
                }
                else{
                    p = &(*p)->next;
                }
            }
            return head;
        }
    };




  • 相关阅读:
    数据结构(树链剖分):NOI2014 购票
    数据结构(树链剖分):COGS 2109. [NOIP2015] 运输计划
    数据结构(树链剖分,堆):HNOI 2016 network
    快速傅里叶变换(FFT):COGS 2216. 你猜是不是KMP
    生成树的计数(基尔霍夫矩阵):BZOJ 1002 [FJOI2007]轮状病毒
    数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo
    数位DP:SPOJ KPSUM
    动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
    数据结构(并查集):COGS 260. [NOI2002] 银河英雄传说
    生成树的计数(基尔霍夫矩阵):UVAoj 10766 Organising the Organisation SPOJ HIGH
  • 原文地址:https://www.cnblogs.com/zhxshseu/p/0f5ebbcd3cee26f69f1cf7914b9ef9d6.html
Copyright © 2020-2023  润新知