LintCode - 删除排序链表中的重复元素(普通)

版权声明:本文为博主原创文章,未经博主允许不得转载。

难度:容易
要求:

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

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

思路

/**
     * @param ListNode head is the head of the linked list
     * @return: ListNode head of linked list
     */
    public static ListNode deleteDuplicates(ListNode head) { 
        if(head == null){
            return null;
        }
        ListNode node = head;
        int tmpDel = head.val;
        while(node.next != null){
            if(node.next.val == tmpDel){
                node.next = node.next.next;
            }else{
                node = node.next;
                tmpDel = node.val;
            }
        }
        return head;
    } 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容