要求

  • 在链表中删除值为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的更多相关文章

  1. 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题要求 ...

  2. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  3. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

  4. 【刷题-LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...

  5. 【LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...

  6. 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 -- ...

  7. 203. Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  8. (easy)LeetCode 203.Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  9. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

随机推荐

  1. 【DB宝46】NoSQL数据库之CouchBase简介、集群搭建、XDCR同步及备份恢复

    目录 一. CouchBase概述 1.1.简述 1.2.CouchDB和CouchBase比对 1.2.1.CouchDB和CouchBase的相同之处 1.2.2.CouchDB和CouchBas ...

  2. [Fundamental of Power Electronics]-PART I-2.稳态变换器原理分析-2.5/2.6 多极点滤波器电压纹波估计及要点小结

    2.5 含两极点低通滤波器变换器的输出电压纹波估计 在分析包含两极点低通滤波器的变换器如Cuk变换器及Buck变换器(图2.25)输出时,小纹波近似将会失效.对于这些变换器而言,无论输出滤波电容的值是 ...

  3. Java中对象的生与灭- 核心篇

    前言 大家好啊,我是汤圆,今天给大家带来的是<Java中对象的生与灭- 核心篇>,希望对大家有帮助,谢谢 文章纯属原创,个人总结难免有差错,如果有,麻烦在评论区回复或后台私信,谢啦 简介 ...

  4. ABP 适用性改造 - 添加 API 版本化支持

    Overview 在前面的文章里有针对 abp 的项目模板进行简化,构建了一个精简的项目模板,在使用过程中,因为我们暴露的 api 需要包含版本信息,我们采取的方式是将 api 的版本号包含在资源的 ...

  5. 并发编程(共享模型之管程wait notify)

    本文主要讲解wait/notify的正确使用姿势.park/unpark.join()的原理.模式之生产者-消费者模式(异步).保护性暂停模式(同步).线程状态转换的流程.死锁和活锁以及如何检查死锁等 ...

  6. Spring Boot demo系列(二):简单三层架构Web应用

    2021.2.24 更新 1 概述 这是Spring Boot的第二个Demo,一个只有三层架构的极简Web应用,持久层使用的是MyBatis. 2 架构 一个最简单的Spring Boot Web应 ...

  7. k8s 运行单实例 mysql

    配置文件mysql.yaml --- apiVersion: v1 kind: Service metadata: name: mysql-01 spec: ports: - port: 3306 s ...

  8. Spring Security极简入门三部曲(上篇)

    目录 Spring Security极简入门三部曲(上篇) 写在前面 为什么要用Spring Security 数据库设计 demo时刻 核心代码讲解 小结 Spring Security极简入门三部 ...

  9. 【Java集合】JDK1.7和1.8 HashMap有什么区别

    JDK1.7和1.8 HashMap区别: 1.数组+链表改成了数组+链表或红黑树: 2.表的插入方式从头插法改成了尾插法,简单说就是插入时,如果数组位置上已经有元素,1.7将新元素放到数组中,原始节 ...

  10. C/C++ 手工实现IAT导入表注入劫持

    DLL注入有多种方式,今天介绍的这一种注入方式是通过修改导入表,增加一项导入DLL以及导入函数,我们知道当程序在被运行起来之前,其导入表中的导入DLL与导入函数会被递归读取加载到目标空间中,我们向导入 ...