[刷题] 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 --> ...
随机推荐
- DSP代码搬运至RAM运行
程序运行过程中,有些函数或程序段和数据等经常调用,正常情况下在FLASH中运行处理消耗时间和资源较大,通常将其移植至RAM中运行,可提高运行效率. 如: 1 #pragma CODE_SECTION( ...
- java面试-阻塞队列
一.阻塞队列 当阻塞队列是空,从队列中获取元素的操作会被阻塞 当阻塞队列是满,往队列中添加元素的操作会被阻塞 二.为什么用,有什么好处? 我们不需要关心什么时候需要阻塞线程,什么时候需要唤醒线程,因为 ...
- BUAA_OO_第三单元
一.JML初探 JML(Java Modeling Language)作为一种形式化语言,可以约束Java代码中类和方法的状态和行为形成规格,通过将一系列具体代码实现抽象成明确的行为接口,可以形成 ...
- OO第三单元个人总结
OO第三单元个人总结 JML理论与基础与应用工具链 JML是什么? Java建模语言(JML)是一种行为接口规范语言,可用于指定Java模块的行为 .它结合了Eiffel的契约设计方法 和Larch ...
- 消息中间件-RabbitMQ基本使用
RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的.所有主要 ...
- C语言小知识(基于Linux)——个人笔记,不定时更新
一.switch case语法,在case中定义变量时,需要在case的有效范围内使用花括号包起来,否则会编译报错: switch (name){ case "zhangSan": ...
- Day06_31_接口(Interface)
java 接口(Interface) 接口和抽象类的区别? 子类只能通过extends关键字去继承抽象类(Abstract),子类(如果不是抽象类)则必须覆写抽象类之中的全部抽象方法(如果子类没有实现 ...
- VUE+Element 前端应用开发框架功能介绍
前面介绍了很多ABP系列的文章<ABP框架使用>,一步一步的把我们日常开发中涉及到的Web API服务构建.登录日志和操作审计日志.字典管理模块.省份城市的信息维护.权限管理模块中的组织机 ...
- 0702-计算机视觉工具包torchvision
0702-计算机视觉工具包torchvision 目录 一.torchvision 概述 二.通过 torchvision 加载模型 三.通过 torchvision 加载并处理数据集 四.通过 to ...
- NodeJS中的LRU缓存(CLOCK-2-hand)实现
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文参考:https://www.codeproject.com/Articles/5299328/LRU- ...