/**
* 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.
*/
public class S83 { public static void main(String[] args) {
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(1);
n1.next = n2;
ListNode n3 = new ListNode(2);
n2.next = n3; ListNode x = deleteDuplicates(n1);
x.print();
} public static ListNode deleteDuplicates(ListNode head) {
ListNode base = head; // base 为每次被比较的对象
if(head==null || head.next == null){
return head;
}
ListNode cur = head.next; // cur为每次去比较的对象 while(base!=null && cur!=null){
if(base.val == cur.val){ // 重复了就跳过去
base.next = cur.next;
}else{ // 不重复就更新base
base = base.next;
}
cur = cur.next; // 更新cur
} return head;
}
}
												

Remove Duplicates from Sorted List @LeetCode的更多相关文章

  1. Remove Duplicates from Sorted Array [LeetCode]

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

  2. Remove Duplicates From Sorted Array leetcode java

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

  3. Remove Duplicates from Sorted List leetcode java

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

  4. Remove Duplicates From Sorted Array

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

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

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

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

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

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

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

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

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

  9. LeetCode:Remove Duplicates from Sorted List I II

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

随机推荐

  1. $^,$@,$?,$<,$(@D),$(@F) of makefile

    makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F)代表的不同含义 $(filter-out $(PHONY) $(wildcard $^),$^)常用 ...

  2. spring事务传播属性与隔离级别

    一.Propagation (事务的传播属性) Propagation : key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为. 有以下选项可供使用: PROPAGATIO ...

  3. poj 1659 Frogs' Neighborhood(出入度、可图定理)

    题意:我们常根据无向边来计算每个节点的度,现在反过来了,已知每个节点的度,问是否可图,若可图,输出一种情况. 分析:这是一道定理题,只要知道可图定理,就是so easy了  可图定理:对每个节点的度从 ...

  4. HDU 5344 MZL's xor (水题)

    题意:给一个序列A,设序列B的中的元素有(Ai+Aj)(1≤i,j≤n),那么求B中所有元素的异或之和.而序列A是这样来的:A1=0,Ai=(Ai−1∗m+z) mod l. 思路:相同的元素异或结果 ...

  5. NTP时间服务器配置与解析

    NTP时间服务器配置与解析 Edit By ZhenXing_Yu 目 录 编译安装ntp server 2 修改ntp.conf配置文件 2 配置时间同步客户机 2 在服务端验证: 3 在客户端进行 ...

  6. 学习macos常用的一些快捷键笔记

    学习mac 操作系统使用笔记 Dock功能学习 类似快捷图标一样 Command+q quit a program Dock上添加与删除都用拖动 command+delete 删除文件 shift+c ...

  7. Android View绘制13问13答

    1.View的绘制流程分几步,从哪开始?哪个过程结束以后能看到view? 答:从ViewRoot的performTraversals开始,经过measure,layout,draw 三个流程.draw ...

  8. 深入学习Struts2

    本部分主要介绍struts.xml的常用配置. 1.1.    包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管理Action和拦截器等.每个包就是多个A ...

  9. Android Application 深入分析

    http://blog.csdn.net/rain_butterfly/article/details/37598939

  10. Delphi 之前解析串口数据

    //串口接收数据procedure TfrmClientMain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Wo ...