# [leetcode][list] Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
while (p != NULL){
ListNode *q = p->next;
while (q != NULL && q->val == p->val){
ListNode *tmp = q;
q = q->next;
delete tmp;
}
p->next = q;
p = p->next;
}
}
};

• 本文已收录于以下专栏：

## Leetcode: Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb...

## [leetcode]83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For examp...

## LeetCode 83 - Remove Duplicates from Sorted List

Remove Duplicates from Sorted List题目链接 https://leetcode.com/problems/remove-duplicates-from-sorted...

## [LeetCode]Remove Duplicates from Sorted List II

Question Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dis...

## 【LeetCode】83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List Total Accepted: 111368 Total Submissions: 304182 Difficu...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)