zoukankan      html  css  js  c++  java
  • 203. Remove Linked List Elements

    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

    此题考查的是如何删除链表中某些节点,属于基本的链表题,直接上代码:

    /**

     * Definition for singly-linked list.

     * public class ListNode {

     *     int val;

     *     ListNode next;

     *     ListNode(int x) { val = x; }

     * }

     */

    public class Solution {

        public ListNode removeElements(ListNode head, int val) {

            ListNode dummy = new ListNode(0);

            dummy.next = head;

            ListNode pre = dummy;

            ListNode cur = head;

            while(cur!=null){

                if(cur.val==val){

                    pre.next = cur.next;

                    cur = cur.next;

                }else{

                    cur = cur.next;

                    pre = pre.next;

                }

            }

            return dummy.next;

        }

    }

  • 相关阅读:
    markdown
    线段树模板
    Trie模板 UVALive 3942 Remember the Word
    使用swift写sprite Kit的模仿微信打飞机游戏
    Graffiti support page
    使用代码控制ScrollView的contentSize
    资料整理
    pd.to_sql()用法
    如何将表格的数据导入到mysql
    安装启动MySQL8.0,报错:1053
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6354142.html
Copyright © 2011-2022 走看看