Remove Duplicates from Sorted List | & ||
Remove Duplicates from Sorted List I
Given a sorted linked list, delete all duplicates such that each element appear only once.
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.
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/
最新文章
- 游戏服务器菜鸟之C#初探四游戏服务
- 基础知识javascript--事件
- 【BZOJ】3922: Karin的弹幕
- 【MySQL】MySQL server has gone away 怎么处理?
- apt-get命令详解
- B. Factory Repairs--cf627B(线段树)
- [OpenCV] 4、图像叠加 addWeighted
- Hadoop core-site.xml 配置项列表
- 【IOS学习基础】weak和strong、懒加载、循环引用
- java线程 — 创建和启动线程
- Linux网络设备驱动(一) _驱动模型
- 【原创】C# API 未能创建 SSL/TLS 安全通道 问题解决
- (转)Oracle定时执行计划任务
- Android 程序崩溃之后fragment出现画面重叠问题
- Eclipse xml中自动提示,添加 dtd或xsd依赖
- winform获取EXE图片
- java数据类型关系及关系
- java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application-dev.yml'
- django练习题
- webview与js交互(转)