【Leetcode】【Easy】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
题目很简单,注意链表首结点有可能更改时,需新建preHead结点,或者使用二维指针的编程方法。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if (head == NULL)
return head; ListNode* prehead = new ListNode();
prehead->next = head;
ListNode* curNode = prehead; while (curNode->next != NULL) {
if (curNode->next->val == val)
curNode->next = curNode->next->next;
else
curNode = curNode->next;
} head = prehead->next;
delete prehead;
return head;
}
};
【Leetcode】【Easy】Remove Linked List Elements的更多相关文章
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 【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 l ...
- 【LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode--LinkedList--203. Remove Linked List Elements(Easy)
203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
随机推荐
- ORC Files
ORC 全称是Optimized Row Columnar,意思是优化的RC file,优化行列式. ORC 文件格式提供了一个很高效的方式来存储hive数据.它旨在克服其他hive文件格式的限制.当 ...
- cadence help文件库调出指令 :cdnshelp
cadence help文件库调出指令 :cdnshelp 指令参数记录: Verilog 添加可编译文件后缀名 -vlog_ext +.h,.vh Verilog1995 添加可编译文件后缀名 -v ...
- mysql故障总结
MYSQL故障排查 https://zhuanlan.zhihu.com/p/27834293
- 我的Python升级打怪之路【一】:python的简单认识
Python的简介 Python与其他语言的对比: C和Python.Java.C# C语言:代码直接编译成了机器码,在处理器上直接执行 Python.Java.C#:编译得到相应的字节码,虚拟机执行 ...
- ORACLE迁移GP实践
最近在做oracle到greenplum的迁移实践,步骤如下: 1. 使用ora2pg实现Oracle的数据结构迁移到GP的实现过程 2. Oracle的数据迁移到GP的实现过程 1. ora2p ...
- 【随笔】 MyEclipse2014的安装和破解
MyEclipse,是在eclipse 基础上加上了自己的插件.MyEclipse,是在eclipse 基础上加上自己的插件开发而成的功能强大的企业级集成开发环境,主要用于Java.Java EE以及 ...
- lua三目运算符
lua的类似三目运算符用法 一般化的Lua三目运算为:(a and {b} or {c})[1] local v = (a and {b} or {c})[1]如果a为true,则 v = b 如果a ...
- 8.28_the end
HomeWork the first level 1. 盒模型 #parent { width: 100px; height: 100px; background:black; overflow: h ...
- Visual Studio 2013复制项目
在当前解决方案下复制项目的步骤: 1. 在硬盘存放代码的目录下将整个文件拷贝一份,修改文件夹名字,改成新的项目名称, 然后修改 *.csproj文件,名字必须与新项目名一致. 2. 在解决方案上右键 ...
- HDU 5694——BD String——————【递归求解】
BD String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...