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的更多相关文章

  1. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  2. 【leetcode刷题笔记】Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  3. 【leetcode刷题笔记】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. 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 ...

  5. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  6. 算法题丨Remove Duplicates from Sorted Array

    描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...

  7. <LeetCode OJ> 83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...

  8. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  9. 【leetcode刷题笔记】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  10. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

随机推荐

  1. windows 下编译64位ffmpeg

    window下如何编译ffmpeg 前文已有介绍,前面介绍的都是如何编译32位的库,本文主要介绍window 下如何编译64位ffmpeg库. 一.搭建编译环境 1.下载 windows下编译64位F ...

  2. 使用itchat分析自己的微信(1)

    1.准备工作 安装itchat pip install itchat参考 itchat内部函数 2.个人微信男女比例分析 ''' Have fun:itchat Author:杨 景 Time:201 ...

  3. web开发中的路径问题

    http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html 转自:http://www.blogjava.net/meil/arc ...

  4. python中常用的base64 md5 aes des crc32等的加密解密

    1.base64 Python内置的base64模块可以实现base64.base32.base16.base85.urlsafe_base64的编码解码,python 3.x通常输入输出都是二进制形 ...

  5. sublime使用技巧(1)-- 下载与插件安装

    一.下载 到官网下载最新的版本 https://www.sublimetext.com/ Sublime官方插件网站 https://packagecontrol.io/ 二.插件安装配置 1.打开S ...

  6. shell 获取文件名

    1.获取文件名并修改文件名 2.$@ 遍历参数 3.赋值要加"" 4.if 判断注意空格 else后面不能跟then

  7. 很全的php数组操作方法

    一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...

  8. Swift_4_闭包(Blocks)

    import Foundation println("Hello, World!") var arr = [1,2,4,6,74,2] func hasClosure(list:[ ...

  9. 【BZOJ4184】shallot 线段树+vector+线性基

    [BZOJ4184]shallot Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从 ...

  10. Sumdiv(较难数学题)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20971   Accepted: 5290 Description Cons ...