【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
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的更多相关文章
- 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)
[LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- 【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 ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【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 ...
- 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 ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 【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 ...
随机推荐
- SQL中的Null深入研究分析
SQL中的Null深入研究分析 虽然熟练掌握SQL的人对于Null不会有什么疑问,但总结得很全的文章还是很难找,看到一篇英文版的, 感觉还不错. Tony Hoare 在1965年发明了 null 引 ...
- Database File Management ->> Shrink Data File
今天在开发环境遇到了一个问题,我们发现服务器上的硬盘空间满了,查看了下发现这个盘存放的数据库文件应该是来源一个并非很大的库才对.检查之后发现这个数据库下的某个数据文件占了盘符下70%的空间,而大部分数 ...
- PowerDesigner导出表到word
一.模版修改 在导出表时,powerdesigner默认为我们提供了很多的模版,在工具栏中选择[Report--->Report Template]//// [被翻译成报告(Report)--- ...
- 你猜……你再猜
『男』:你喜欢我吗? 『女』:你猜. 『男』:喜欢. 『女』:你再猜. 『男』:--
- poj-3616 Milking Time (区间dp)
http://poj.org/problem?id=3616 bessie是一头工作很努力的奶牛,她很关心自己的产奶量,所以在她安排接下来的n个小时以尽可能提高自己的产奶量. 现在有m个产奶时间,每个 ...
- JAVA设计模式之【工厂方法模式】
看例子 1.TV产品接口,负责播放 public interface TV // TV接口 { public void play(); } 2.TV工厂接口,负责生产产品 public interfa ...
- Javascript学习-闭包
看的这篇 http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html 各种专业文献上的"闭包" ...
- UVa 11100 The Trip, 2007
今天的教训:做题要用大块的时间来做,上午做一下,做题做到一半就去忙别的事,那么后面再做的时候就无限CE,WA了.因为你很难或者需要很长时间来找回当时的思路. 题意:就像套瓷娃娃一样,有n个包,大小可能 ...
- HDU 2064 (递推) 汉诺塔III
将柱子从左到右依次编号为A.B.C 设将n个盘子从一端移动到另一端的最少步数为f(n) 则f(n)和f(n-1)的递推关系为:f(n) = 3 × f(n-1) + 2 初始状态A柱子上面有n个盘子, ...
- HDU 1016 Prime Ring Problem
在刚刚写完代码的时候才发现我以前交过这道题,可是没有过. 后来因为不理解代码,于是也就不了了之了. 可说呢,那时的我哪知道什么DFS深搜的东西啊,而且对递归的理解也很肤浅. 这道题应该算HDU 261 ...