[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 II

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

【LeetCode】Remove Duplicates from Sorted List

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

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

[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 ***

题目 题意 注意 思路 代码题目 Given a sorted linked list, delete all duplicates such that each element appear o...

LeetCode Remove Duplicates from Sorted List II

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

LeetCode(83)Remove Duplicates from Sorted List

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

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

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

[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个字)