成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Remove Linked List Elements

huayeluoliuhen / 2213人閱讀

Problem

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

Example

Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

Solution
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        // Write your code here
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        head = dummy;
        while (head.next != null) {
            if (head.next.val == val) {
                head.next = head.next.next;
            }
        else {
            head = head.next;
            }
        }    
        return dummy.next;
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65432.html

相關(guān)文章

  • [LeetCode/LintCode] Remove Nth Node From End of L

    Problem Given a linked list, remove the nth node from the end of list and return its head. Example Given linked list: 1->2->3->4->5->null, and n = 2. After removing the second node from the end, the l...

    Jaden 評(píng)論0 收藏0
  • [LintCode] Remove Duplicates form Sorted List I &a

    摘要:和上一道題不同的地方就是,需要用雙指針操作,且最后要返回,以防止結(jié)點(diǎn)即的情況返回錯(cuò)誤的結(jié)果。令,用進(jìn)行查重操作,是的前結(jié)點(diǎn)。當(dāng)和等值的時(shí)候,后移至第一個(gè)不等值的點(diǎn),用指向新的即可。 Remove Duplicates form Sorted List I Problem Given a sorted linked list, delete all duplicates such tha...

    int64 評(píng)論0 收藏0
  • [LintCode/LeetCode] LRU Cache

    摘要:方法繼承了的很多方法,本身的方法有對(duì)運(yùn)行速度影響不大,隨意設(shè)定是默認(rèn)值,為浮點(diǎn)數(shù)為,與意思相同最近過的 Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. ge...

    walterrwu 評(píng)論0 收藏0
  • 148. Sort List

    摘要:題目分析一看到問題,而且時(shí)間復(fù)雜度要求又是,很自然地就會(huì)想到數(shù)組時(shí),如下這道題要求是,所以在上面的基礎(chǔ)上還要進(jìn)行一些額外操作找到的中點(diǎn),使用快慢指針法。需要注意的是,找到中點(diǎn)后要把鏈表分成兩段,即兩個(gè)鏈表。這部分代碼應(yīng)該近似于這道題的答案。 Sort a linked list in O(n log n) time using constant space complexity. 題...

    anquan 評(píng)論0 收藏0
  • [LintCode/LeetCode] Convert Sorted List to Balance

    摘要:當(dāng)鏈表為空時(shí),中出現(xiàn)大于,返回。然后計(jì)算中點(diǎn),以為界分別遞歸構(gòu)建左右子樹。順序是,左子樹根結(jié)點(diǎn)右子樹。由于根節(jié)點(diǎn)是直接取構(gòu)建,當(dāng)前的已經(jīng)被取用。所以在下一次遞歸構(gòu)建右子樹之前,要讓指向。最后將和左右子樹相連,返回。 Problem Given a singly linked list where elements are sorted in ascending order, conve...

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

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

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<