[刷题] 203 Remove Linked List Elements
要求
- 在链表中删除值为val的所有节点
示例
- 如 1->2->3->4->5->6->NULL,要求删除值为6的节点
- 返回1->2->3->4->5->NULL
思路
- 删除一般元素(包括最后一个元素)
- 删除第一个元素

实现
- 常规思路
1 #include <iostream>
2 using namespace std;
3
4 struct ListNode {
5 int val;
6 ListNode *next;
7 ListNode(int x) : val(x), next(NULL) {}
8 };
9
10 ListNode* createLinkedList(int arr[], int n){
11 if( n == 0 )
12 return NULL;
13 ListNode* head = new ListNode(arr[0]);
14 ListNode* curNode = head;
15 for( int i = 1 ; i < n ; i ++ ){
16 curNode->next = new ListNode(arr[i]);
17 curNode = curNode->next;
18 }
19 return head;
20 }
21
22 void printLinkedList(ListNode* head){
23 ListNode* curNode = head;
24 while( curNode != NULL ){
25 cout << curNode->val << " -> ";
26 curNode = curNode->next;
27 }
28 cout<<"NULL"<<endl;
29 return;
30 }
31
32 void deleteLinkedList(ListNode* head){
33 ListNode* curNode = head;
34 while( curNode != NULL){
35 ListNode* delNode = curNode;
36 curNode = curNode->next;
37 delete delNode;
38 }
39 return;
40 }
41
42 class Solution {
43 public:
44 ListNode* removeElements(ListNode* head, int val) {
45
46 while( head != NULL && head->val == val ){
47 ListNode* delNode = head;
48 head = delNode->next;
49 delete delNode;
50 }
51
52 if( head == NULL )
53 return NULL;
54
55 ListNode* cur = head;
56 while( cur->next != NULL ){
57
58 if( cur->next->val == val ){
59 ListNode* delNode = cur->next;
60 cur->next = delNode->next;
61 delete delNode;
62 }else
63 cur = cur->next;
64 }
65 return head;
66 }
67 };
68
69 int main(){
70 int arr[] = {1,2,3,4,5};
71 int n = sizeof(arr)/sizeof(int);
72
73 ListNode* head = createLinkedList(arr,n);
74 Solution().removeElements(head,3);
75 printLinkedList(head);
76
77 deleteLinkedList(head);
78 return 0;
79 }
- 设置虚拟头节点

1 #include <iostream>
2 using namespace std;
3
4 struct ListNode {
5 int val;
6 ListNode *next;
7 ListNode(int x) : val(x), next(NULL) {}
8 };
9
10 ListNode* createLinkedList(int arr[], int n){
11 if( n == 0 )
12 return NULL;
13 ListNode* head = new ListNode(arr[0]);
14 ListNode* curNode = head;
15 for( int i = 1 ; i < n ; i ++ ){
16 curNode->next = new ListNode(arr[i]);
17 curNode = curNode->next;
18 }
19 return head;
20 }
21
22 void printLinkedList(ListNode* head){
23 ListNode* curNode = head;
24 while( curNode != NULL ){
25 cout << curNode->val << " -> ";
26 curNode = curNode->next;
27 }
28 cout<<"NULL"<<endl;
29 return;
30 }
31
32 void deleteLinkedList(ListNode* head){
33 ListNode* curNode = head;
34 while( curNode != NULL){
35 ListNode* delNode = curNode;
36 curNode = curNode->next;
37 delete delNode;
38 }
39 return;
40 }
41
42 class Solution {
43 public:
44 ListNode* removeElements(ListNode* head, int val) {
45
46 ListNode* dummyHead = new ListNode(0);
47 dummyHead->next = head;
48
49 ListNode* cur = dummyHead;
50 while( cur->next != NULL ){
51
52 if( cur->next->val == val ){
53 ListNode* delNode = cur->next;
54 cur->next = delNode->next;
55 delete delNode;
56 }else
57 cur = cur->next;
58 }
59
60 ListNode* retNode = dummyHead->next;
61 delete dummyHead;
62
63 return retNode;
64 }
65 };
66
67 int main(){
68 int arr[] = {1,2,3,4,5};
69 int n = sizeof(arr)/sizeof(int);
70
71 ListNode* head = createLinkedList(arr,n);
72 Solution().removeElements(head,3);
73 printLinkedList(head);
74
75 deleteLinkedList(head);
76 return 0;
77 }
相关
- 82 Remove Duplicates from Sorted List II
- 21 Merge Two Sorted Lists
[刷题] 203 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题要求 ...
- 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 --> ...
随机推荐
- 前端使用bcrypt对密码加密,服务器对密码进行校验
以前为了防止前端密码安全问题,都是对密码进行md5(password + salt). 有些也会用别的加密方式,但还是会存在撞库,彩虹表等破解常规密码. 因此使用bcrypt加密是一个不错的选择,因为 ...
- Kubernetes 常用日志收集方案
Kubernetes 常用日志收集方案 学习了 Kubernetes 集群中监控系统的搭建,除了对集群的监控报警之外,还有一项运维工作是非常重要的,那就是日志的收集. 介绍 应用程序和系统日志可以帮助 ...
- js 日期加减
加: console.log(moment().format("YYYY-MM-DD HH:mm:ss")); //当前时间 console.log(moment().add(10 ...
- java面试-Java内存模型(JMM)
p.p1 { margin: 0; font: 15px Helvetica } 一.并发编程两个关键问题 线程之间如何通信.同步.java并发采用的是共享内存模型 二.JMM内存模型的抽象结构 描述 ...
- 如何在CSS中映射的鼠标位置,并实现通过鼠标移动控制页面元素效果
映射鼠标位置或实现拖拽效果,我们可以在 JavaScript 中做到这一点.但实际上,在CSS中有更加简洁的方法,我们可以在不使用JavaScript 的情况下,仍然可以实现相同的功能! 只使用CSS ...
- SpringCloud(四)GateWay网关
GateWay网关 概述简介 Gateway是在 Spring生态系统之上构建的AP网关服务,基于 Spring5, Spring Boot2和 Project Reactor等技术. Gateway ...
- Python 第二章-列表和元组
第二章-列表和元组 2.0 在Python中,最基本的数据结构是序列(sequence).序列中的每个元素被分配一个序列号-即元素的位置, 也称为索引.第一个索引是0,第二个是1,以此类推. ...
- UVA11021麻球繁衍
题意: 有K只麻球,每只生存一天就会死亡,每只麻球在死之前有可能生下一些麻球,生i个麻球的概率是pi,问m天后所有的麻球都死亡的概率是多少? 思路: 涉及到全概率公式,因为麻球的 ...
- SQL注入注释符(#、-- 、/**/)使用条件及其他注释方式的探索
以MySQL为例,首先我们知道mysql注释符有#.-- (后面有空格)./**/三种,在SQL注入中经常用到,但是不一定都适用.笔者在sqlilabs通关过程中就遇到不同场景用的注释符不同,这让我很 ...
- 每天一道面试题LeetCode 26--删除排序数组中的重复项(python实现)
题目1:给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. ...