83.Remove Duplicates from Sorted List(删除链表重复元素,STL容器map)

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

For example,

Given 1->1->2, return 1->2.

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

HideTags

Linked List

#pragma once
#include<iostream>
#include<map>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

ListNode *deleteDuplicates(ListNode *head)
{
	ListNode* p = head;
	ListNode* pp = NULL;//指向p的前一个节点
	map<int, int> m;
	map<int, int>::iterator it;
	while (p)
	{
		//find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
		it = m.find(p->val);
		if (it == m.end())//没找到
		{
			m[p->val] = 1;
			pp = p;
			p = p->next;
		}
		else//找到,已有,删除p指向的节点
		{
			p = p->next;
			(*pp).next = p;
		}
	}
	return head;
}

void main()
{
	ListNode* l1 = new ListNode(1);
	ListNode* l2 = new ListNode(1);
	ListNode* l3 = new ListNode(2);
	ListNode* l4 = new ListNode(3);
	ListNode* l5 = new ListNode(3);
	ListNode* l6 = NULL;
	l1->next = l2;
	l2->next = l3;
	l3->next = l4;
	l4->next = l5;
	ListNode* result = deleteDuplicates(l1);
	while (result)
	{
		cout << result->val << " ";
		result = result->next;
	}
	cout << endl;
	system("pause");
}
时间: 2024-10-31 06:50:19

83.Remove Duplicates from Sorted List(删除链表重复元素,STL容器map)的相关文章

leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法

Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.

(Java) LeetCode 83. Remove Duplicates from Sorted List —— 删除排序链表中的重复元素

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 很简单的链表问题,可以写成递归和迭代两种形式.具体思路: 第一步,寻找第一个节点值和当前表头所指的节

[leetcode]83. Remove Duplicates from Sorted List有序链表去重

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 题意: 有序链表去重 思路: 代码: 1 class Solution { 2 public Lis

leetCode 83. Remove Duplicates from Sorted List 链表

83. 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. 题目大意: 去除有序链表内部相同元素,即相同

&amp;lt;LeetCode OJ&amp;gt; 83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: Easy 题目意思:如今有一个已经排好顺序的链表,删除全部反复的节点.使每一个节点都仅仅出现一次! Given a sorted linked list, delete all duplicates such that each element appear only once. For exampl

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

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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-&

leetcode:83 Remove Duplicates from Sorted List-每日编程第十六题

Remove Duplicates from Sorted List Total Accepted: 89961 Total Submissions: 253975 Difficulty: Easy 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-&g

Remove Duplicates from Sorted 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. public ListNode deleteDu

[LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序数组中删除重复项)

描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2Output: 1->2Example 2: Input: 1->1->2->3->3Output: 1->2->3 有序链表去重. 解析 移除给定有序链表的重复项,那么我们可以遍历这个链表,每个结点和其后面的结点比较

Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]

题目: 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. 翻译: 给定一个排序号的链表,删除所有的重复元素,保证每个元素只出现一次. 分析: 在当前节点删除下一节点,会