Remove Duplicates from Sorted List I

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

Example

Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

分析:

Use one pointer called "current" to point to the head, and pointer "pointer" points to the next one. If the value of node pointed by "pointer" is the same with the value of the node pointed by pointer "current", we move pointer "pointer" to the next node, otherwise, current.next = pointer, and both pointers move to the next node.

 /**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if (head == null || head.next == null) return head; ListNode current = head;
ListNode pointer = head.next; while (pointer != null) {
if (pointer.val != current.val) {
current.next = pointer;
current = pointer;
}
pointer = pointer.next;
}
current.next = null;
return head;
}
}

Remove Duplicates from Sorted List II

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

Example

Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

分析:

This question is a little bit hard. It is because we need to move the pointer all the way down to the node whose value is not the same with the previous one.

a if and while loop combination can be used in this case.

 /**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of the linked list
*/ public static ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) return head; ListNode dummy = new ListNode();
ListNode current = dummy; while (head != null) {
if (head.next != null && head.val == head.next.val) {
int val = head.val;
while(head != null && head.val == val) {
head = head.next;
}
} else {
current.next = head;
current = current.next;
head = head.next;
current.next = null;
}
}
return dummy.next;
}
}

转载请注明出处:cnblogs.com/beiyeqingteng/

最新文章

  1. 游戏服务器菜鸟之C#初探四游戏服务
  2. 基础知识javascript--事件
  3. 【BZOJ】3922: Karin的弹幕
  4. 【MySQL】MySQL server has gone away 怎么处理?
  5. apt-get命令详解
  6. B. Factory Repairs--cf627B(线段树)
  7. [OpenCV] 4、图像叠加 addWeighted
  8. Hadoop core-site.xml 配置项列表
  9. 【IOS学习基础】weak和strong、懒加载、循环引用
  10. java线程 — 创建和启动线程
  11. Linux网络设备驱动(一) _驱动模型
  12. 【原创】C# API 未能创建 SSL/TLS 安全通道 问题解决
  13. (转)Oracle定时执行计划任务
  14. Android 程序崩溃之后fragment出现画面重叠问题
  15. Eclipse xml中自动提示,添加 dtd或xsd依赖
  16. winform获取EXE图片
  17. java数据类型关系及关系
  18. java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application-dev.yml'
  19. django练习题
  20. webview与js交互(转)

热门文章

  1. Spring-涉及到的设计模式汇总
  2. 【poj3020】 Antenna Placement
  3. POJ 1740 A New Stone Game
  4. android studio中the logging tag can be most 23 characters
  5. iOS开发的那些坑
  6. 常用数据库 JDBC URL 格式
  7. mysql join详解
  8. 防止ajax请求重发
  9. 转:Java NIO系列教程(二) Channel
  10. DS_Store 是什么文件