203. Remove Linked List Elements (List)
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.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
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->next = cur->next;
}
else{
pre = pre->next;
}
cur = cur->next;
}
return newHead->next;
}
};
203. Remove Linked List Elements (List)的更多相关文章
- 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题要求 ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
- 【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* ...
- Java for LeetCode 203 Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java [Leetcode 203]Remove Linked List Elements
题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- [LeetCode] 203. Remove Linked List Elements 解题思路
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
随机推荐
- leetcode1025
public class Solution { public bool DivisorGame(int N) { == ) { return false; } else { return true; ...
- DOS 格式化日期时间输出
if "%date:~5,2%" lss "10" (set mm=0%date:~6,1%) else (set mm=%date:~5,2%)if &quo ...
- python 读取文件
python 一次读取多行 with open(filename, "r") as f: lines = f.readlines(LINE_BATCH) while lines: ...
- git reset 版本回退
git log 查看所有提交信息. commit 67692318180bed6b2a17db0708cfbe0231e33db3 (HEAD -> master) Author: kingBo ...
- [PHP]Nginx与PHP的文件上传大小限制
---------------------------------------------------------------------------------------------------- ...
- Android中查看SQLite中字段数据的两种方式
方式一:ADB Pull 通过adb pull导出*.db文件到PC的文件夹中,通过可视化工具 SQLiteExpertPers 进行查看.编辑: adb pull /data/data/com.jo ...
- [原创]HTML 用div模拟select下拉框
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML xmlns=" ...
- 线程执行synchronized同步代码块时再次重入该锁过程中抛异常,是否会释放锁
一个线程执行synchronized同步代码时,再次重入该锁过程中,如果抛出异常,会释放锁吗? 如果锁的计数器为1,抛出异常,会直接释放锁: 那如果锁的计数器为2,抛出异常,会直接释放锁吗? 来简单测 ...
- Memory Controller
1.S3C6410 地址空间 For memory at the table details 2. SDRAM图解 3. SDRAM 芯片容量计算: L_BANK 数目 * 一个 L_BANK 中的单 ...
- JVM G1GC参数配置
https://www.oracle.com/technetwork/articles/java/g1gc-1984535.html 主要这两个参数需要设置下,ParallelGCTh ...