您的位置:首页 > 编程语言 > Java开发

leetCode练习(83)

2016-10-17 17:05 399 查看
题目:Remove Duplicates from Sorted List

难度:easy

问题描述:

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
.

解题思路:

链表新手题···没啥好说的。直接看代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode temp,last;
if(head==null||head.next==null){
return head;
}
temp=head.next;
last=head;
while(true){
if(temp.val==last.val){
last.next=temp.next;
temp=temp.next;
}else{
last=temp;
temp=temp.next;
}
if(temp==null){
break;
}
}
return head;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  List java leetcode