您的位置:首页 > 其它

Lintcode删除排序链表中的重复元素

2017-09-30 19:41 274 查看


删除排序链表中的重复元素 

 描述
 笔记

 数据

 评测

给定一个排序链表,删除所有重复的元素每个元素只留下一个。

您在真实的面试中是否遇到过这个题? 

Yes

样例

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

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


public class Solution {

    /*

     * @param head: head is the head of the linked list

     * @return: head of linked list

     */

    public ListNode deleteDuplicates(ListNode head) {

        // write your code here

        if(head==null){

            return null;

        }

        ListNode a=head;            //记录头结点用于返回。

        while(head.next!=null){

            if(head.val==head.next.val){

                head.next=head.next.next;

            }else{

            head=head.next;

        }}

        return a;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: