力扣 题目82+83- 删除排序链表中的重复元素I/II

题目

83

82

题解

今天打开力扣一看怎么82在83前面呢 果断先做83

83

我们需要三个节点

分别是 头结点(head):返回用 遍历节点(temporary):遍历每一个节点 保存节点(replace):针对题目中的情况进行修改节点 replacenum:记录每一个节点的数

当temporary->val != replacenum 即当前节点和上一个节点数值不一样则修改 保存节点

1 if (temporary->val != replacenum) {
2                 replacenum = temporary->val;
3                 replace->next = temporary;
4                 replace = replace->next;
5             }

最后别忘了replace->next = NULL;

82题

首先我们发现头结点有些特殊 那么我们在前面加入一个临时节点(temporaryhead) 这样头结点就可以当作普通节点处理

1         ListNode*temporaryhead= new(ListNode);
2         temporaryhead->val = -101;
3         temporaryhead->next = head;
4         

然后我们发现其实就是在83上增加判断即 如果temporary->val != replacenum 的话此时 如果当前节点是最后一个节点或者当前节点的下一个节点的值不与当前节点的值相等 则添加

1 if (temporary->val != replacenum) {
2                 replacenum = temporary->val;
3                 if (temporary->next == NULL ||(temporary->next != NULL && temporary->val != temporary->next->val)) {
4                     replace->next = temporary;
5                     replace = replace->next;
6                 }
7             }

最后返回 temporaryhead->next;

代码

82

 1 #include<iostream>
 2 #include<vector>
 3 #include<stack>
 4 using namespace std;
 5 struct ListNode {
 6     int val;
 7     ListNode* next;
 8     ListNode() : val(0), next(nullptr) {}
 9     ListNode(int x) : val(x), next(nullptr) {}
10     ListNode(int x, ListNode* next) : val(x), next(next) {}
11 };
12 
13 
14 class Solution {
15 public:
16     ListNode* deleteDuplicates(ListNode* head) {
17         if (head == nullptr) {
18             return head;
19         }
20 
21         ListNode*temporaryhead= new(ListNode);
22         temporaryhead->val = -101;
23         temporaryhead->next = head;
24         
25         ListNode* temporary = temporaryhead;
26         ListNode* replace = temporaryhead;
27         int replacenum = temporaryhead->val;
28         for (int i = 0; temporary != NULL; i++) {
29             if (temporary->val != replacenum) {
30                 replacenum = temporary->val;
31                 if (temporary->next == NULL ||(temporary->next != NULL && temporary->val != temporary->next->val)) {
32                     replace->next = temporary;
33                     replace = replace->next;
34                 }
35             }
36             temporary = temporary->next;
37         }
38         replace->next = NULL;
39         return temporaryhead->next;
40     }
41 };
42 void listnum(ListNode& list, vector<int>& vectorlist) {
43     ListNode* headlist = &list;
44     for (int i = 0; i < vectorlist.size(); i++) {
45         ListNode* p;
46         p = (struct ListNode*)malloc(sizeof(struct ListNode*));
47         p->val = vectorlist[i];
48         headlist->next = p;
49         headlist = headlist->next;
50     }
51     headlist->next = NULL;
52 }
53 int main() {
54     Solution sol;
55     ListNode list1(1);
56     vector<int> vectorlist1 = { 2,3,3,4,5 };
57     listnum(list1, vectorlist1);
58     ListNode* list = sol.deleteDuplicates(&list1);
59     for (int i = 0; list != NULL; i++) {
60         cout << list->val << endl;
61         list = list->next;
62     }
63 }
View Code

83

 1 #include<iostream>
 2 #include<vector>
 3 #include<stack>
 4 using namespace std;
 5 struct ListNode {
 6     int val;
 7     ListNode* next;
 8     ListNode() : val(0), next(nullptr) {}
 9     ListNode(int x) : val(x), next(nullptr) {}
10     ListNode(int x, ListNode* next) : val(x), next(next) {}
11 };
12 
13 
14 class Solution {
15 public:
16     ListNode* deleteDuplicates(ListNode* head) {
17         if (head == nullptr) {
18             return head;
19         }
20         ListNode* temporary = head;
21         ListNode* replace = head;
22         int replacenum = head->val;
23         for (int i = 0; temporary != NULL; i++) {
24             if (temporary->val != replacenum) {
25                 replacenum = temporary->val;
26                 replace->next = temporary;
27                 replace = replace->next;
28             }
29             temporary = temporary->next;
30         }
31         replace->next = NULL;
32         //ListNode*temporary= (struct ListNode*)malloc(sizeof(struct ListNode*));
33         //temporary->val = -1;
34         //temporary->next = head;
35 
36         return head;
37     }
38 };
39 void listnum(ListNode& list, vector<int>& vectorlist) {
40     ListNode* headlist = &list;
41     for (int i = 0; i < vectorlist.size(); i++) {
42         ListNode* p;
43         p = (struct ListNode*)malloc(sizeof(struct ListNode*));
44         p->val = vectorlist[i];
45         headlist->next = p;
46         headlist = headlist->next;
47     }
48     headlist->next = NULL;
49 }
50 int main() {
51     Solution sol;
52     ListNode list1(1);
53     vector<int> vectorlist1 = { 2,3,3,4,4,5,6,6 };
54     listnum(list1, vectorlist1);
55     ListNode* list = sol.deleteDuplicates(&list1);
56     for (int i = 0; list != NULL; i++) {
57         cout << list->val << endl;
58         list = list->next;
59     }
60 }
View Code

 

posted @ 2022-07-06 15:15  无聊的阿库娅  阅读(69)  评论(0编辑  收藏  举报