(LeetCode 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
题目要求:
删除链表中包含val的元素结点
解题思路:
重点在于找到第一个非val的头结点,然后遍历链表,依次删除值为val的结点,最后返回头结点
方法:
1、常规方法:
找到第一个非val的头结点,如果头结点非NULL,遍历链表,依次删除值为val的结点,最后返回头结点。
2、递归方法:
代码:
/**
* 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) {
while(head!=NULL && head->val==val)
head=head->next;
if(head==NULL)
return head;
// At least one node that does not contain val
ListNode* cur;
cur=head;
while(cur->next!=NULL){
if(cur->next->val==val)
cur->next=cur->next->next;
else
cur=cur->next;
}
return head;
}
};
/**
* 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 && head->val==val)
head=removeElements(head->next,val);
if(head && head->next)
head->next=removeElements(head->next,val);
return head;
}
};
(LeetCode 203)Remove Linked List Elements的更多相关文章
- LeetCode算法题-Remove Linked List Elements(Java实现)
这是悦乐书的第189次更新,第191篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第48题(顺位题号是203).移除单链表中节点值为val的节点.例如: 输入:1-> ...
- [LC]203题 Remove Linked List Elements (移除链表元素)(链表)
①英文题目 Remove all elements from a linked list of integers that have value val. Example: Input: 1-> ...
- (LinkedList) Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode OJ :Remove Linked List Elements (移除链表元素)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- (LeetCode 83)Remove Duplicates from Sorted Lists
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【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 、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 - 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. ...
随机推荐
- Needed Learning(Updating)
决定把掌握不熟练或是模型见的少的知识点在这里列一列 希望能在自己AFO前成功get技能点吧…… 优先级:动态规划-分治-字符串-图论-数据结构-数学-计算几何-其它 动态规划 1.四边形不等式优化 2 ...
- 【漏洞预警】CVE-2017-8464 震网三代漏洞复现
早在6月13日,微软发布补丁修复编号为CVE-2017-8464的漏洞,本地用户或远程攻击者可以利用该漏洞生成特制的快捷方式,并通过可移动设备或者远程共享的方式导致远程代码执行,追溯到以前,NSA就承 ...
- AVL树理解
AVL树理解 简介 我们知道,AVL树也是平衡树中的一种,是自带平衡条件的二叉树,始终都在维护树的高度,保持着树的高度为logN,同时把插入.查找.删除一个结点的时间复杂度的最好和最坏情况都维持在O( ...
- pygame系列_pygame安装
在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,所以把标题设置为pygame系列 在这篇blog中,主要描述一下我们怎样来安装pygame 可能很多人像我一样,发现了pygame是 ...
- 离线下载Xcode的文档
https://developer.apple.com/library/downloads/docset-index.dvtdownloadableindex 找到里面的文档下载地址 例如iOS 8. ...
- Content portal for Pocketables Tasker articles
http://www.pocketables.com/2013/03/overview-of-pocketables-tasker-articles.html I write a lot about ...
- ICD2 VPP limiter for new PIC microcontrollers.
http://www.circuitsathome.com/mcu/pic_vpp_limiter VOUT = 2.5V * ( 1 + 24/10 ) = 2.5 * 3.4 = 8.5V New ...
- PHP自学之路---雇员管理系统(1)
前面已经介绍了Zend studio工具的使用以及软件开发的基本阶段,下面就是我们第一个练习,雇员管理系统,从设计到实现来简单介绍下: 开发环境: 服务器:基于Linux 2.618环境下配置PHP服 ...
- MYSQL 源码- 淘宝数据库研发组
http://mysql.taobao.org/index.php?title=%E8%B5%84%E6%96%99%E5%85%B1%E4%BA%AB
- 用最简单的例子理解备忘录模式(Memento Pattern)
简单来说,备忘录模式就是支持回退操作.假设让一个Notepad支持回退操作,如何实现呢? 首先需要一个备忘录类. public class Memento { private string _msg; ...