2020-01-27刷题

题目链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/

题目说明:

83. 删除排序链表中的重复元素

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2

示例 2:

输入: 1->1->2->3->3
输出: 1->2->3


题目代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode() : val(0), next(nullptr) {}
 7  *     ListNode(int x) : val(x), next(nullptr) {}
 8  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 9  * };
10  */
11 class Solution {
12 public:
13     ListNode* deleteDuplicates(ListNode* head) {
14         ListNode * p = head;
15         while( p && p->next ){
16         if(p->val == p->next->val){
17             ListNode *del = p->next; 
18             p->next = p->next->next;
19             delete del;
20         }else{
21             p = p->next;
22         }
23     }
24     return head;
25 }
26 };
原文地址:https://www.cnblogs.com/gjianli/p/14336413.html