LeetCode - 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
.
思路:
package list;public class RemoveDuplicatesFromSortedList {public ListNode deleteDuplicates(ListNode head) {ListNode p = head;ListNode start = head;while (head != null && head.next != null) {ListNode headNext = head.next;if (head.next.val != head.val) {start = head.next;} else {start.next = head.next.next;}head = headNext;}return p;}public static void main(String[] args) {// TODO Auto-generated method stubListNode a1 = new ListNode(1);ListNode a2 = new ListNode(1);ListNode a3 = new ListNode(1);ListNode a4 = new ListNode(1);ListNode a5 = new ListNode(1);a1.next = a2;a2.next = a3;a3.next = a4;a4.next = a5;a5.next = null;RemoveDuplicatesFromSortedList r = new RemoveDuplicatesFromSortedList();ListNode head = r.deleteDuplicates(a1);while (head != null) {System.out.println(head.val);head = head.next;}}}
LeetCode - Remove Duplicates from Sorted List相关推荐
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- [LeetCode] Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- 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 - 链表问题
题目概述: Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- LeetCode Remove Duplicates from Sorted List II
题意:给出一个单链表 ,将其中重复的元素删除 思路:在找重复结点时,需要找到其前继结点 代码如下: class Solution {public ListNode deleteDuplicates(L ...
- LeetCode() Remove duplicates from sorted list II
ListNode* dummy = new ListNode(0); //必须要加上 new ListNode(0); 否则有错误. dummy->next = head; head = dum ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array
LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
最新文章
- usaco Riding the Fences(欧拉回路模板)
- 移动端手机网站建设应注意哪些问题?
- python 非法字符处理
- mysql 使用中 修复 blog_Java My-Blog之mysql容器重复初始化严重bug修复过程
- 简易计算器里的小数点在程序中怎么表示_财管普通计算器(内有彬哥经验)
- 引导界面图标好大_游戏里那些图标和界面,原来是这么设计出来的?
- React开发(191):ant design中inputNumber格式化
- 小学奥数 7828 最大公约数与最小公倍数 python
- stm32F051系列教程 前哨篇 建立一个KEIL工程模板
- 译:理解 Win32 OutputDebugString
- Kubernetes Pod
- jdk1.8的安装与环境变量配置
- js动态获取屏幕宽高度
- 华硕主板怎么进入bios
- Ubuntu后台启动任务,关闭对话窗口不退出
- 第十四周 项目1抽象基类
- H.264中NALU、RBSP、SODB的关系 (弄清码流结构)
- SL651-2014全协议解析
- R 报错:参数不是数值也不是逻辑值:回覆NA--数据科学新类型tibble
- QQ好友自定义头像不更新的解决办法(转)
热门文章
- java迷宫算法继承_求Java关于迷宫的算法(用栈实现)
- springboot定时备份MYSQL_spring boot 定时备份数据库
- ic卡读卡器软件_读卡器
- mysql 1500万_【IT专家】mysql分表后 如何分页 (总共160个表1500万数据)
- 微宝球型机器人功能_腾讯智能球型机器人专属app(微宝)
- java form上传图片_js formData图片上传(单图上传、多图上传)后台java
- php foreach 过滤条件_PHP+Redis实现延迟任务实现完成订单
- 宝塔更换域名_搭建小程序之BT宝塔面板的操作使用教程
- 【leetcode】722. Remove Comments
- 设计模式之一(单例模式)