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

資訊專欄INFORMATION COLUMN

[Leetcode] Delete Node/Remove Element in a Linked

Cruise_Chan / 1567人閱讀

摘要:賦值法復(fù)雜度時(shí)間空間思路乍一看沒(méi)法獲取上一個(gè)鏈表節(jié)點(diǎn),似乎無(wú)法將當(dāng)前結(jié)點(diǎn)去除。實(shí)際上只要將下一個(gè)節(jié)點(diǎn)的值覆蓋當(dāng)前節(jié)點(diǎn),然后刪除下一個(gè)節(jié)點(diǎn)就好了。注意這樣不適用于尾節(jié)點(diǎn)。

Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

賦值法 復(fù)雜度

時(shí)間 O(1) 空間 O(1)

思路

乍一看沒(méi)法獲取上一個(gè)鏈表節(jié)點(diǎn),似乎無(wú)法將當(dāng)前結(jié)點(diǎn)去除。實(shí)際上只要將下一個(gè)節(jié)點(diǎn)的值覆蓋當(dāng)前節(jié)點(diǎn),然后刪除下一個(gè)節(jié)點(diǎn)就好了。注意這樣不適用于尾節(jié)點(diǎn)。

代碼
public class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}
Remove Linked List Elements 偽造表頭 復(fù)雜度

時(shí)間 O(N) 空間 O(1)

思路

刪除鏈表所有的特定元素的難點(diǎn)在于如何處理鏈表頭,如果給加一個(gè)dummy表頭,然后再?gòu)膁ummy表頭開(kāi)始遍歷,最后返回dummy表頭的next,就沒(méi)有這么問(wèn)題了。

代碼
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode curr = dummy;
        while(curr.next!=null){
            if(curr.next.val == val){
                curr.next = curr.next.next;
            } else {
                curr = curr.next;
            }
        }
        return dummy.next;
    }
}

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

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

相關(guān)文章

  • LeetCode707:設(shè)計(jì)鏈表 Design Linked List

    摘要:愛(ài)寫(xiě)設(shè)計(jì)鏈表的實(shí)現(xiàn)。單鏈表中的節(jié)點(diǎn)應(yīng)該具有兩個(gè)屬性和。插入后,新節(jié)點(diǎn)將成為鏈表的第一個(gè)節(jié)點(diǎn)。將值為的節(jié)點(diǎn)追加到鏈表的最后一個(gè)元素。如果等于鏈表的長(zhǎng)度,則該節(jié)點(diǎn)將附加到鏈表的末尾。如果索引有效,則刪除鏈表中的第個(gè)節(jié)點(diǎn)。操作次數(shù)將在之內(nèi)。 愛(ài)寫(xiě)bug (ID:iCodeBugs) 設(shè)計(jì)鏈表的實(shí)現(xiàn)。您可以選擇使用單鏈表或雙鏈表。單鏈表中的節(jié)點(diǎn)應(yīng)該具有兩個(gè)屬性:val 和 next。val 是...

    iliyaku 評(píng)論0 收藏0
  • LeetCode707:設(shè)計(jì)鏈表 Design Linked List

    摘要:愛(ài)寫(xiě)設(shè)計(jì)鏈表的實(shí)現(xiàn)。單鏈表中的節(jié)點(diǎn)應(yīng)該具有兩個(gè)屬性和。插入后,新節(jié)點(diǎn)將成為鏈表的第一個(gè)節(jié)點(diǎn)。將值為的節(jié)點(diǎn)追加到鏈表的最后一個(gè)元素。如果等于鏈表的長(zhǎng)度,則該節(jié)點(diǎn)將附加到鏈表的末尾。如果索引有效,則刪除鏈表中的第個(gè)節(jié)點(diǎn)。操作次數(shù)將在之內(nèi)。 愛(ài)寫(xiě)bug (ID:iCodeBugs) 設(shè)計(jì)鏈表的實(shí)現(xiàn)。您可以選擇使用單鏈表或雙鏈表。單鏈表中的節(jié)點(diǎn)應(yīng)該具有兩個(gè)屬性:val 和 next。val 是...

    FullStackDeveloper 評(píng)論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語(yǔ)言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...

    張漢慶 評(píng)論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)

    摘要:月下半旬攻略道題,目前已攻略題。目前簡(jiǎn)單難度攻略已經(jīng)到題,所以后面會(huì)調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時(shí),攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚(yú)有什么區(qū)別...

    tain335 評(píng)論0 收藏0
  • [LeetCode] 426. Convert BST to Sorted Doubly Linke

    Problem Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Lets take the foll...

    MartinDai 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<