237 - Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

Hide Tags: Linked List

struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
};
void deleteNode(ListNode *node)
{
/*the main problem is that we can not know the pre ListNode of node,
so we have to copy the val one by one*/
ListNode *cur=node, *post=cur->next;
while(post)
{
cur->val=post->val;
if(post->next==NULL)
cur->next=NULL;
else
cur=post;
post=cur->next;
}
}

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

ListNode* removeElements(ListNode* head, int val)
{
ListNode *newhead=new ListNode(-);
newhead->next=head;
ListNode *pre=newhead;
ListNode *cur=head;
while(cur)
{
if(cur->val!=val)
pre=pre->next;
else
pre->next=cur->next;
cur=cur->next;
}
}

【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements的更多相关文章

  1. 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)

    [LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  2. 【leetcode】1019. Next Greater Node In Linked List

    题目如下: We are given a linked list with head as the first node.  Let's number the nodes in the list: n ...

  3. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  4. 【LeetCode】237. Delete Node in a Linked List

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  5. LeetCode(237)Delete Node in a Linked List

    题目 Write a function to delete a node (except the tail) in a singly linked list, given only access to ...

  6. 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...

  7. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  8. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  9. 【leetcode】955. Delete Columns to Make Sorted II

    题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...

随机推荐

  1. SQL中的Null深入研究分析

    SQL中的Null深入研究分析 虽然熟练掌握SQL的人对于Null不会有什么疑问,但总结得很全的文章还是很难找,看到一篇英文版的, 感觉还不错. Tony Hoare 在1965年发明了 null 引 ...

  2. Ext的正则表达式

    http://www.cnblogs.com/azai/archive/2010/12/31/1923140.html   今天看到一篇关于Extjs正则表达式比较系统的总结. 使用extJs时能常用 ...

  3. Java异常 —— java.lang.NoClassDefFoundError

    一直使用 Eclipse 来开发 Java . 现学习 Maven,在 cmd 下使用 Java ,出现了这样的异常:Exception in thread "main" java ...

  4. ACdream 1735 输油管道 (排序)

    http://acdream.info/problem?pid=1735 官方题解:http://acdream.info/topic?tid=4246 因为主干线是平行于x轴的直线,那么跟x坐标其实 ...

  5. javascript 一个关于时间排序的算法(一个页面多个倒计时排序)

    上周要做一个活动页面 秒杀列表页 需要一个时间的算法排序 自己琢磨了半天想了各种算法也没搞出来,后来问了下一个后台的php同学 他写了个算法给我看了下 ,刚开始看的时候觉得这就是个纯算法,不能转化成页 ...

  6. objective-c 与 js之间传递中文乱码

    最近在做关于js改写oc framework的小project,遇到了不少问题 其中刚遇到的是关于如何在两者之间传递中文字符,带特殊字符的URL字符串 不会很详细的介绍太多,以后会回头做个总结 oc传 ...

  7. 使用Jenkins构建持续集成环境

    简介 Jenkins是一个开源的持续集成工具,提供了数百种插件供用户选择,能够完成整套持续集成环境的构建. 它具有如下的特点: 持续集成和持续发布 作为可扩展的自动服务器,Jenkins可以作为简单的 ...

  8. eclipse中字母大小写转换快捷键

    ctrl+shift+x   转为大写 ctrl+shift+y   转为小写

  9. HDU 3544 (不平等博弈) Alice's Game

    切巧克力的游戏,想得还是不是太明白. 后者会尽量选前着切后其中小的一块来切,那么先手须尽量取中间来切. So?题解都是这么一句话,不知道是真懂了还是从别人那抄过来的. 后来找到一个官方题解,分析得比较 ...

  10. Asp.Net保存session的三种方法

    C#中保存Session的三种方法及Web.Config设置 1.保存session到sql server,需要指定Sql Server服务器,这种方法因为要读写数据库最慢 <sessionSt ...