【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 天的价格. ...
随机推荐
- 浅谈struts2标签中的2个非经常常使用的标签的使用方法(radio和select)
1.如图所看到的我们须要在前台的页面通过radio和select将相应的数据库中的数据显示到选项其中,这也是我们做项目中常常须要做的,动态的显示,而不是静态的显示. 首先我们须要在页面中导入strut ...
- asp.net 导出excel 中文乱码解决方法 (转)
用我转载的上一篇文章 Asp.net中把DataTable或DataGrid导出为Excel 导出的文档,中文有乱码现象,其实要解决中文乱码很简单,设置一下字符集.如下: // 设置编码和附件格式 c ...
- 无法连接到服务器,用户xxx登陆失败"
无法连接到服务器,用户xxx登陆失败" 该错误产生的原因是由于SQL Server使用了"仅 Windows"的身份验证方式, 因此用户无法使用SQL Server的登录 ...
- spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet
spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet 情况 ...
- full join 时通过辅助列序号列消除笛卡尔积重复列
如果没有序号列,那么如果领灯表里有3条数据,还灯表里面有2条数据,full join后就是3*2=6条数据 --1.领灯表,每天每班每人允许重复数据 select ID ,ROW_NUMBER() o ...
- MongoDB启动及用户名密码设置
1.服务启动 下载后的安装步骤,请参见mongoDB安装详细教程 启动服务NET START MongoDB 关闭服务NET STOP MongoDB 启动客户端mongo MongoDB shell ...
- CSS 选择器 :last-child与:last-of-type的区别
:last-child----represents the last element among a group of sibling elements. CSS伪类 :last-child 代表在一 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- DuiVision开发教程(18)-弹出窗
DuiVision的弹出窗体类CDlgPopup,是菜单.下拉列表等控件的父类,也能够单独使用,用于创建弹出窗体.弹出窗体默认是非激活状态下自己主动关闭,比如鼠标点击到弹出窗体外面的区域,弹出窗体就会 ...
- [译]GLUT教程(目录)
http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...