成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] Remove LinkedList Elements

sherlock221 / 335人閱讀

摘要:鏈表基本的刪除操作,最好掌握以下三種方法。

Problem

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

Note

鏈表基本的刪除操作,最好掌握以下三種方法。

Solution Dummy Node
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return null;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy, cur = head;
        while (cur != null) {
            if (cur.val == val) pre.next = cur.next;
            else pre = pre.next;
            cur = cur.next;
        }
        return dummy.next;
    }
}
Recursion
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return head;
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;
    }
}
Iteration
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val) head = head.next;
        ListNode cur = head;
        while (cur != null && cur.next != null) {
            if (cur.next.val == val) cur.next = cur.next.next;
            else cur = cur.next;
        }
        return head;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/64771.html

相關(guān)文章

  • LeetCode 203:移除鏈表元素 Remove LinkedList Elements

    摘要:刪除鏈表中等于給定值的所有節(jié)點(diǎn)。鏈表的刪除操作是直接將刪除節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)指向刪除節(jié)點(diǎn)的后一個(gè)節(jié)點(diǎn)即可。這就無需考慮頭節(jié)點(diǎn)是否為空是否為待刪除節(jié)點(diǎn)。 刪除鏈表中等于給定值 val 的所有節(jié)點(diǎn)。 Remove all elements from a linked list of integers that have value val. 示例: 輸入: 1->2->6->3->4->5-...

    hzc 評論0 收藏0
  • LeetCode 203:移除鏈表元素 Remove LinkedList Elements

    摘要:刪除鏈表中等于給定值的所有節(jié)點(diǎn)。鏈表的刪除操作是直接將刪除節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)指向刪除節(jié)點(diǎn)的后一個(gè)節(jié)點(diǎn)即可。這就無需考慮頭節(jié)點(diǎn)是否為空是否為待刪除節(jié)點(diǎn)。 刪除鏈表中等于給定值 val 的所有節(jié)點(diǎn)。 Remove all elements from a linked list of integers that have value val. 示例: 輸入: 1->2->6->3->4->5-...

    makeFoxPlay 評論0 收藏0
  • [Leetcode] Combination Sum 組合數(shù)之和

    摘要:深度優(yōu)先搜索復(fù)雜度時(shí)間空間遞歸棧空間思路因?yàn)槲覀兛梢匀我饨M合任意多個(gè)數(shù),看其和是否是目標(biāo)數(shù),而且還要返回所有可能的組合,所以我們必須遍歷所有可能性才能求解。這題是非?;厩业湫偷纳疃葍?yōu)先搜索并返回路徑的題。本質(zhì)上是有限深度優(yōu)先搜索。 Combination Sum I Given a set of candidate numbers (C) and a target number (...

    GitCafe 評論0 收藏0
  • [Leetcode] Sliding Window Maximum 滑動窗口最大值

    摘要:這樣,我們可以保證隊(duì)列里的元素是從頭到尾降序的,由于隊(duì)列里只有窗口內(nèi)的數(shù),所以他們其實(shí)就是窗口內(nèi)第一大,第二大,第三大的數(shù)。 Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from the very left of the array to...

    lvzishen 評論0 收藏0
  • [Leetcode] Zigzag Iterator Z形迭代器

    摘要:用變量和取模來判斷我們該取列表中的第幾個(gè)迭代器。同樣,由于每用完一個(gè)迭代器后都要移出一個(gè),變量也要相應(yīng)的更新為該迭代器下標(biāo)的上一個(gè)下標(biāo)。如果迭代器列表為空,說明沒有下一個(gè)了。 Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. For exa...

    SolomonXie 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<