[刷题] 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 --> ...
随机推荐
- [Fundamental of Power Electronics]-PART I-5.不连续导电模式-5.2 变比M分析
5.2 变比M分析 经过一些改进,第二章中的用于CCM稳态分析的相同技术和近似方法可以应用于DCM. (a)电感伏秒平衡.电感电压直流分量必须为0: \[<v_{L}>=\frac{1}{ ...
- 设计原则:开闭原则(OCP)
1.什么是开闭原则 开闭原则的英文是Open Closed Principle,缩写就是OCP.其定义如下: 软件实体(模块.类.方法等)应该"对扩展开放.对修改关闭". 从定义上 ...
- Git版本控制之-创建配置本地git仓库
查看全局配置:code .gitconfig [code 就代表的用vscode 打开gitconfig 文件,如果是 sublime 就是 subl ][如果打开失败说明环境变量没有配置] [只有配 ...
- Mybatis的多表操作
1.Mybatis多表查询 1.1 一对一查询 1.1.1 一对一查询的模型MapperScannerConfigurer 用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一 ...
- Oracle recover current redo ORA-00600:[4193] (oracle 故障恢复current redo日志ORA-00600:[4193]报错)
背景:搭建了一套oracle 19c主备库(单实例非CDB,PDB),linux7.5在断电后(没有进行数据库关闭)重启数据库报错如下图,redo当前状态下进行不完全恢复主库后resetlogs 打开 ...
- Buuctf刷题:部分
get_started_3dsctf_2016 关键词:ROP链.栈溢出.mprotect()函数 可参考文章(优质): https://www.cnblogs.com/lyxf/p/12113401 ...
- Linux Nvidia显卡驱动安装
1 概述 因为某些需要需要在Linux上安装显卡驱动,这里记录一下安装过程. 2 环境 Manjaro RTX 2060 3 下载驱动安装包 到官网上搜索下载即可,可以戳这里: 选择自己的显卡型号即可 ...
- kubernetes 查看cpu,内存使用情况
kubectl top pod --all-namespaces kubectl top pod -n kubeflow
- 深入浅出:MySQL的左连接、右连接、等值连接
深入浅出:MySQL的左连接.右连接.等值连接 三种连接的语法 为便于更多的技友快速读懂.理解,我们只讨论2张表对象进行连接操作的情况,大于2张表对象进行的连接操作原理也是一样的. 1.左连接(LEF ...
- Linux下部署Django项目
目录 安装python3.X环境 安装部署开启django 由于Linux系统默认自带的是2.X环境,所以我们需要去安装3.X环境的python. 安装python3.X环境 1.使用下面的命令下载P ...