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/

Remove Duplicates from Sorted List | & ||的更多相关文章

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

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

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

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

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

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

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

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

  5. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  6. Remove Duplicates from Sorted List II

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

  7. Remove Duplicates From Sorted Array

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

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 26. Remove Duplicates from Sorted Array

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

  10. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

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

随机推荐

  1. log4j:WARN Please initialize the log4j system properly.解决

    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA ...

  2. WPF小知识,MessageBox的多种用法

    我们在程序中经常会用到MessageBox. 现将其常见用法总结如下: 1.MessageBox.Show("Hello~~~~"); 最简单的,只显示提示信息. 2.Messag ...

  3. bootstrap fileinput添加上传成功回调事件

    国外牛人做的bootstrap fileinput挺酷的,但是可惜没有提供自定义上传成功回调事件的接口,因此感到非常头疼,但是很幸运的是,我在网上搜索到一个提问帖子,它问到使用Jquery的on函数绑 ...

  4. No message found under code ' for locale 'en'.

    1.如果你使用eclipse创建的工程是class和src分开的,那么资源属性文件一定要放在src目录以内.2.属性文件名的写法:messages_zh_CN.properties (中文)messa ...

  5. 【bzoj4010】 HNOI2015—菜肴制作

    http://www.lydsy.com/JudgeOnline/problem.php?id=4010 (题目链接) 题意 给出一张无向图要求出一个拓扑序列满足1的位置最靠前 ,在保证上面的条件下使 ...

  6. BZOJ3732 Network

    Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_ ...

  7. BZOJ1083 繁忙的都市

    Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口 ...

  8. sql-server数据库中利用触发器实现表与表之间的级联删除

    create trigger Delete_Student --创建一个触发器 on student instead of delete as declare @sno varchar() selec ...

  9. hdu 1202 The calculation of GPA

    感觉本题没有什么好解释的,已知公式,直接编程即可. 1.统计所有 科目的学分 得到总学分 2.统计所有 成绩对应的绩点*对应的学分即可(如果成绩==-1直接continue,不进行统计),得到总绩点. ...

  10. python购物&常用字符处理方法

    python 一个购物车的例子 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 '''购物车''' 4 5 goods = [ 6 7 {&quo ...