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;

        }

    }

  • 相关阅读:
    Canvas基础讲义
    封装一个DivTag
    递归深拷贝
    构造函数的执行过程
    封装一个Ajax工具函数
    数组去重
    [js开源组件开发]js多选日期控件
    自己写的表格插件autotable
    复杂表格的树形结构的添加删除行div实现
    自制html5塔防游戏
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6354142.html
Copyright © 2011-2022 走看看