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

来源:互联网 发布:淘宝网男包包 编辑:程序博客网 时间:2024/06/07 04:47

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

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

样例

给出 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;
    }
}
原创粉丝点击