国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode] Remove Linked List Elements

huayeluoliuhen / 2206人閱讀

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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/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 評論0 收藏0
  • [LintCode] Remove Duplicates form Sorted List I &a

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

    int64 評論0 收藏0
  • [LintCode/LeetCode] LRU Cache

    摘要:方法繼承了的很多方法,本身的方法有對運行速度影響不大,隨意設(shè)定是默認(rè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 評論0 收藏0
  • 148. Sort List

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

    anquan 評論0 收藏0
  • [LintCode/LeetCode] Convert Sorted List to Balance

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

    Michael_Ding 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<