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.

Hide Tags

Linked List

 

  1. class Solution {
  2. public:
  3. ListNode *deleteDuplicates(ListNode *head) {
  4. if(head==NULL) return NULL;
  5. ListNode *lp = head,*rp = head;
  6. while(rp!=NULL){
  7. while(rp!=NULL&&rp->val==lp->val) rp=rp->next;
  8. lp->next = rp;
  9. lp = rp;
  10. }
  11. return head;
  12. }
  13. };

[LeetCode] Remove Duplicates from Sorted List 链表的更多相关文章

  1. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  2. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  3. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  4. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

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

  5. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  6. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  7. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  8. Leetcode: Remove Duplicates from Sorted List II 解题报告

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  9. [LeetCode]Remove Duplicates from Sorted Array题解

    Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...

随机推荐

  1. c 语言技巧

    位运算 & 位逻辑与 | 位逻辑或 ^ 位逻辑异或 - 位逻辑反 >> 右移 << 左移 通过对数据本身的01编码进行处理,速度稍微快于普通运算符 如,10 / 2 = ...

  2. Session 会话保持

    本文将详细讨论session的工作机制并且对在Java web application中应用session机制时常见的问题作出解答 一.术语session session,中文经常翻译为会话,其本来的 ...

  3. PHP导出成PDF功能开发教程

    准备工作 首先查询了相关的类库,有FPDF,zendPDF,TcPDF等等.首先看了下先选择了FPDF,可以说除了中文字符以外没有什么问题,中文乱码而且看了下最新版本没有很好的解决方案,所以只能放弃. ...

  4. js字符串去掉所有空格

    字符串去掉所有空格 "abc 123 def".replace(/\s/g, "") 字符串去掉左右两端空格 " abc 123 def " ...

  5. 【CSS】非常简单的css实现div悬浮页面底部

    <div id="demo_div"></div> <style> #demo_div{ left:; position: fixed; bot ...

  6. 【JavaScript】修改图片src属性切换图片

    今天做项目时其中一个环节需要用到js修改图片src属性切换图片,现在来记录一下 以下是示例: html <img src="/before.jpg" id="img ...

  7. java中常用的swing组件 (2013-10-27-163 写的日志迁移

    五种布局:   流式布局(FlowLayout)边界布局(borderLayout)网格布局(GridLayout)  盒子布局(BoxLaYout)  空布局(null)  常用的几种 卡片布局(C ...

  8. python 2.7版本解决TypeError: 'encoding' is an invalid keyword argument for this function

    今天在用yaml处理数据时,由于yaml.load可接收一个byte字符串,unicode字符串,打开的二进制文件或文本文件对象,但字节字符串和文件必须是utf-8,utf-16-be或utf-16- ...

  9. [BZOJ3684][拉格朗日反演+多项式求幂]大朋友和多叉树

    题面 Description 我们的大朋友很喜欢计算机科学,而且尤其喜欢多叉树.对于一棵带有正整数点权的有根多叉树,如果它满足这样的性质,我们的大朋友就会将其称作神犇的:点权为\(1\)的结点是叶子结 ...

  10. Poj3061Subsequence

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...