题目

删除链表中等于给定值val的所有节点。

样例

给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5

解题

加入头结点进行删除

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @param val an integer
* @return a ListNode
*/
public ListNode removeElements(ListNode head, int val) {
// Write your code here
if(head == null)
return head;
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; }
}

lintcode:删除链表中指定元素的更多相关文章

  1. [LintCode]删除链表中的元素

    问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, in ...

  2. LintCode之删除链表中的元素

    题目描述 我的代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n ...

  3. LintCode #452 删除链表中的元素

    方法很笨拙,被链表给绕住了,抽空在整理一下. /** * Definition for ListNode * public class ListNode { * int val; * ListNode ...

  4. 203 Remove Linked List Elements 删除链表中的元素

    删除链表中等于给定值 val 的所有元素.示例给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6返回: 1 --& ...

  5. 【LintCode】删除链表中的元素

    问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, in ...

  6. LeetCode Delete Node in a Linked List (删除链表中的元素)

    题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...

  7. 删除链表中的元素 · Remove Linked List Elements

    [抄题]: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

  8. javaScript 删除数组中指定元素

    Array.prototype.indexOf = function(val) { for (var i = 0; i < this.length; i++) { if (this[i] == ...

  9. [LintCode] 删除链表中倒数第n个节点

    /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(in ...

随机推荐

  1. [转]Ubuntu中root用户和user用户的相互切换

    [转]Ubuntu中root用户和user用户的相互切换 http://www.cnblogs.com/weiweiqiao99/archive/2010/11/10/1873761.html Ubu ...

  2. win7虚拟打印驱动开发注意事项

    win7虚拟打印驱动开发注意事项 通过控制面板安装遇到以下问题:错误1.提示“Printer driver was not installed. Operation could not be comp ...

  3. [小技巧]初次接触 SSIS Package 的一点总结

    1 动态改变数据源 package从创建到调试到测试到生产环境,往往需要运行在不同的服务器上.我们可以定义Environment和Server两个变量,一个用于改变 Server,一个用于接收实际Se ...

  4. 位bit——字节Byte???

    1.换算 每8个位(bit)组成一个字节(byte) 位bit简写为小写字母“b”,字节Byte简写为大写字母“B” 8*b=1*B 1024*B=1*KB 1024*K=1MB 2.举例 一个英文字 ...

  5. linux matplotlib入门

    python linux matplotlib 安装:   sudo apt-get install python-numpy 必须 先安装numpy matplotlib 安装:   sudo ap ...

  6. 【Insertion Sorted List】cpp

    题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...

  7. cas 登陆超时 解决方案

    在配置文件ticketExpirationPolicies.xml中配置: <bean id="grantingTicketExpirationPolicy" class=& ...

  8. 阻止浏览器关闭 区分刷新和关闭 自试IE可用

    window.onbeforeunload = onbeforeunload_handler; function onbeforeunload_handler(){ if(event.clientX& ...

  9. ios frame、bound和center定义及使用场景总结

    frame:指的是视图在父视图的坐标系统中的大小和位置. bound:指的是视图在视图本身的坐标系统中的大小(位置起点是原点). center:指的是视图在父视图坐标系统中的中心点. frame和bo ...

  10. Appstore提交 被拒绝

    Reasons 16.1: Apps that present excessively objectionable or crude content will be rejected 16.1 We ...