083. Remove Duplicates from Sorted List
题目链接:https://leetcode.com/problems/rotate-list/description/
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
思路:
- 有序的链表中删除有重复值的结点。
- 需要两个指针分别指向链表的第一个结点和第二个结点(如果存在),这两个指针分别为pre和cur; 判断这两个指针所指向结点的值是否相等。
- 若相等,则改变指针指向对多余元素进行删除;
- 若不等,则pre = cur ; cur = cur->next;对指针指向进行移动。
注意:应考虑到链表的尾节点为多余元素的情况如何处理!!!
编码如下:
/**
* 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) {
if (head == nullptr) return head; ListNode *cur = head;
ListNode *pre = nullptr; while ( cur->next != nullptr)
{
pre = cur;
cur = cur->next;
while ( cur != nullptr && pre->val == cur->val)
{
pre->next = cur->next;
cur = cur->next;
} // 应考虑到链表的尾节点为多余元素的情况
if (cur == nullptr)
{
return head;
}
} return head;
}
};
083. Remove Duplicates from Sorted List的更多相关文章
- [LeetCode]题解(python):083 - Remove Duplicates from Sorted List
题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ Given a sorted linked list, d ...
- Java for LeetCode 083 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 083 Remove Duplicates from Sorted List 有序链表中删除重复的结点
给定一个排序链表,删除所有重复的元素使得每个元素只留下一个.案例:给定 1->1->2,返回 1->2给定 1->1->2->3->3,返回 1->2- ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [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 Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- Android异常与性能优化相关面试问题-OOM异常面试问题详解
什么是OOM? 当前占用的内存加上我们申请的内存资源超过了Dalvik虚拟机的最大内存限制就会抛出Out Of Memory异常. 一些容易混淆的概念: 内存溢出:指的就是OOM. 内存抖动:是短时间 ...
- Codeforce Round #424
A 略 B 略 C: 先对Ai数列预处理前缀和 然后把Bi的每个都加一次 最终得到的结果为ans[sum]++; 最后如果有一个ans[sum]>=k即满足所有K个条件就输出(注意!!前缀和要进 ...
- javaWeb中的session和cookie
Cookie Cookie 是浏览器提供的一种技术,通过服务器的程序能将一些只须保存在客户端,或者 在客户端进行处理的数据,放在本地的计算机上,不需要通过网络传输,因而提高网页处理的效率,并且能够减少 ...
- Django:报错 raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
Django 执行迁移生成表: python manage.py migrate 报错: raise MigrationSchemaMissing("Unable to create the ...
- 通过jenkins api远程调用job
curl http://jenkins地址/job/job_name/config.xml --user username:token
- 用 D3.js 画一个手机专利关系图, 看看苹果,三星,微软间的专利纠葛
前言 本文灵感来源于Mike Bostock 的一个 demo 页面 原 demo 基于 D3.js v3 开发, 笔者将其使用 D3.js v5 进行重写, 并改为使用 ES6 语法. 源码: gi ...
- 【原】eclipse连接数据库开发web项目
之前也写过web项目,今天用的时候死活连不上My SQL,浪费了很多时间,下面总结一下: 在java项目里面访问数据库 (1)项目上右击->Build Path->add External ...
- vue中 请求拦截 响应拦截设置
第一,在项目的src中新建http.js文件,将以下代码复制进去 import axios from 'axios' import { Message, Loading } from 'element ...
- 【Winform-自定义控件】可以使用2种半透明的颜色来填充Button
制作一个自定义按钮,使用2种半透明的颜色来填充Button 1.添加一个自定义控件类,并改变基类,继承自Button public partial class CustomControl1 : But ...
- 通过JS完成电梯动画效果
实习单位要求做一个在Vue项目中比较能适配的来反映货梯当前状况的页面效果 用JS写了一个 <!DOCTYPE html> <html> <head> <met ...