(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. ...
随机推荐
- BZOJ.2339.[HNOI2011]卡农(思路 DP 组合 容斥)
题目链接 \(Description\) 有\(n\)个数,用其中的某些数构成集合,求构造出\(m\)个互不相同且非空的集合(\(m\)个集合无序),并满足每个数总共出现的次数为偶数的方案数. \(S ...
- Windows 0day成功验证之ETERNALBLUE
本帖由春秋首发~作者:神风 @春秋文阁负责人 方程式又一波0day[该贴有工具]:https://bbs.ichunqiu.com/thread-21736-1-1.html 最近一段时间出现一波高潮 ...
- Linux 下安装软件包的方法
Linux应用软件的安装包有三种: 1) tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2) rpm包,如software-1.2.3-1. ...
- hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...
- python 爬虫 处理超级课程表传输的数据
借鉴的别人的思路 http://www.oschina.net/code/snippet_2463131_53711 抓取超级课程表传输的数据 他的传输数据居然是明文的-- 现在已经把自己的课表都抓出 ...
- MVC高级编程-目录
MVC高级编程 ================================================== 控制器 视图 模型 表单和HTML辅助方法 数据注解和验证 成员资格.授权和安全性 ...
- asp.net 判断日期是否为空
if (Birthday == DateTime.MinValue) { //u can do something here } 首先确保Birthday是不可为null的日期类型.如果可为null就 ...
- Android 手机 无线 ADB
要用网络调试Android需要设备已经获取root权限 如果手机没有命令行工具,请先在手机端安装终端模拟器,然后在终端输入: $su #stop adbd #setprop service.adb.t ...
- sqlite - Sqlite Wrappers - Delphi
http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers Aducom's SQLite: Open source (NewBSD) Delphi (4. ...
- systemtap 用户态调试2
[root@localhost ~]# cat user.stpprobe process(@1).function(@2){print_ubacktrace();exit();} session 1 ...