【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.
题解:水题不解释
/**
* 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){
while(p->next != NULL && p->next->val == p->val){
ListNode* temp = p->next;
p->next = p->next->next;
delete(temp);
}
if(p!=NULL)
p = p->next;
}
return head;
}
};
Java版本:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return head;
ListNode current = head;
ListNode next = current.next;
while(next != null){
if(current.val == next.val){
//删除next指向的节点
current.next = next.next;
next = current.next;
}else {
current = next;
}
}
return head;
}
}
【leetcode刷题笔记】Remove Duplicates from Sorted List的更多相关文章
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 【leetcode刷题笔记】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- 【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 && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
- <LeetCode OJ> 83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
随机推荐
- linux vi设置行号
首先,vi的配置文件是/etc/vim/vimrc,不同系统可能有出入,不过我建议大家在home中建立一个.vimrc文件,照样可以达到同样的效果.其实/etc中的配置是全局的,home中的配置只针对 ...
- C#开发--FTP操作方法管理
1.整理简化了下C#的ftp操作,方便使用 1.支持创建多级目录 2.批量删除 3.整个目录上传 4.整个目录删除 5.整个目录下载 2.调用方法展示, var ftp ...
- WDCP管理面板忘记ROOT MYSQL密码及重置WDCP后台登录密码方法
不管出于何种原因,应该有不少的朋友在自己的VPS/服务器上采用WDCP管理面板的时候有忘记MYSQL ROOT账户管理密码在寻找解决方法,甚至有忘记WDCP后台管理登录密码的.这些问题都比较简单,只需 ...
- ThinkPHP连接主从数据库
config.php文件设置如下: return array( 'URL_MODE'=>0, 'DB_TYPE'=>'mysql', 'DB_HOST'=>'localhos ...
- .NET CORE 2.0小白笔记(三):数字化平台之微信平台策略
当下,互联网技术正在深刻地重构我们的社会,各大企事业单位——大到万人集团公司,小到图文复印店——都在争先恐后地从所谓的“传统行业”中脱胎换骨一番以完成数字化转型. 在这个过程中,“企业即IT”.“科技 ...
- 固态硬盘(SSD) 和机 械硬盘(HDD) 优缺点比較
Attribute SSD (Solid State Drive) HDD (Hard Disk Drive) Power Draw / Battery Life (功耗/电池寿命) Less pow ...
- linux 下mtime,ctime,atime分析
一.atime.ctime与mtime atime是指access time,即文件被读取或者执行的时间,修改文件是不会改变access time的.网上很多资料都声称cat.more等读取文件的命令 ...
- java严格验证日期是否正确的代码
package com.xxxx.util; /** * 输入日期 并进行验证格式是否正确 */ public class FDate { public static void main(String ...
- 跟我一起写 Makefile(三)[转]
原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) make 的运行—————— 一 ...
- Android BroadcastReceiver介绍 (转)
原文地址:http://www.cnblogs.com/trinea/archive/2012/11/09/2763182.html 本文主要介绍BroadcastReceiver的概念.使用.生命周 ...