您的位置:首页 > Web前端 > Node.js

leetcode-83 Remove Duplicates from Sorted List

2016-11-07 10:10 411 查看

问题描述

地址:https://leetcode.com/problems/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.

翻译:

给出排好序的链表,删除重复的节点,使得每个节点只出现一次。

比如:

给出1->1->2,返回1->2.

给出1->1->2->3->3, 返回1->2->3.

问题解析

解析部分请参照下面的图解:

步骤1:



步骤2:



步骤3:



解析代码

public ListNode deleteDuplicates(ListNode head) {
if(head  == null){
return head;
}
ListNode current = head;
ListNode next = current.next;
while(next != null){
if(current.val == next.val){
current.next = next.next;
next = next.next;

}else{
current = next;
next = next.next;
}
}
return head;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息