[leetcode][list] Remove Duplicates from Sorted List

原创 2015年07月07日 10:17:49

题目:

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.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
	ListNode *p = head;
	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;
	}
	return head;
}
};


相关文章推荐

LeetCode :: Remove Duplicates from Sorted List[详细分析]

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

Remove Duplicates from Sorted List--LeetCode

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

LeetCode82:Remove Duplicates from Sorted List II

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

Remove Duplicates from Sorted List II--LeetCode

题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinc...

[LeetCode] Remove Duplicates from Sorted List II

给定一个已经排序的单链表,删除所有有相同数字的节点,只留下原始链表中的不同数字。...

【一天一道LeetCode】#82. Remove Duplicates from Sorted List II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...

LeetCode - 82. Remove Duplicates from Sorted List II

这一道题目主要是链表的操作,一开始因为情况比较复杂,把自己绕晕了,所以没想到这道题目的解法,后来看了答案才觉得自己解起来确实很难解出。这道题目对于链表的操作比较复杂,所要考虑的情况也很多,最大的一个技...

和大神们学习每天一题(leetcode)-Remove Duplicates from Sorted List

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

leetcode_82_Remove Duplicates from Sorted List II

思路: 大致思路就是,遍历链表找出重复元素的子列并删除重复元素子列,当然,第一个元素开始有重复元素的话比较特种,需要特殊考虑。删除子列的过程稍微有点绕,题目倒是不难理解。...

Leetcode_83_Remove Duplicates from Sorted List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41728739 Given a sorted linked...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode][list] Remove Duplicates from Sorted List
举报原因:
原因补充:

(最多只允许输入30个字)