leetcode 83. Remove Duplicates from Sorted List

来源:互联网 发布:炫酷个人网站php源码 编辑:程序博客网 时间:2024/05/17 16:56

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.

public ListNode deleteDuplicates(ListNode head) {        ListNode resultHead = new ListNode(Integer.MAX_VALUE);        ListNode resultEnd = resultHead;        while(head != null){            if(head.next == null || head.next.val != head.val){                resultEnd.next = head;                head = head.next;                resultEnd = resultEnd.next;                resultEnd.next = null;            }            else{                while(head.next != null && head.val == head.next.val)                    head = head.next;            }        }        return resultHead.next;    }

上一题就删掉一行就行

0 0
原创粉丝点击