Leetcode 83 Remove Duplicates from Sorted List 链表

就是将链表中的重复元素去除

我的方法很简单就是如果链表的前后元素相同的话,将后一个元素删除

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