[刷题] 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 --> ...
随机推荐
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- openGL官方Glut库配置教程
在配置前要先安装好Visual Stdio环境 官方下载网站 注:一台Windows操作系统中可以存在多版本的Visual Stdio,多个版本之间互不干扰但不共享插件库,且高版本向下兼容,因此笔者更 ...
- Python基础(八):字符串的使用(下)
find() 功能:检测字符串是否包含指定字符.如果包含指定字符,则返回开始的索引:否则,返回-1. >>> st = "hello world" >> ...
- 010_Nginx入门
目录 使用场景 什么是Nginx 正向代理与反向代理 正向代理:代理客户端 反向代理:代理服务端 Nginx的作用 反向代理 负载均衡 轮询 加权轮询 IP hash 动静分离 Windows下安装 ...
- redis的持久化有哪几种方式?不同的持久化机制都有什么优缺点?(偏难)
1.RDB和AOF两种持久化机制的介绍 RDB持久化机制,对redis中的数据执行周期性的持久化 AOF机制对每条写入命令作为日志,以append-only的模式写入一个日志文件中,在redis重启的 ...
- Parentheses Balance UVA - 673
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
- Day13_64_线程的优先级
线程的调度 * 单核CPU在某一时刻只能执行一条指令,线程只有得到CPU的时间片,也就是使用权,才可以执行指令 * 在单核CPU的机器上线程不是并行运行的,而是频繁切换运行的,只有在多个CPU上线程才 ...
- 测试报告模板:HTMLTestRunner.py(新版)
报告样式效果: 报告源码:HTMLTestRunner.py 1 """ 2 A TestRunner for use with the Python unit test ...
- 通过ECK部署elasticsearch集群(k8s+elasticsearch+kibana)
参考 https://blog.51cto.com/14783669/2558785
- PAT甲级1100——1155题总结