[Leetcode]083. Remove Duplicates from Sorted List

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 class Solution {
    public ListNode deleteDuplicates(ListNode head) {
         if(head == null || head.next == null)return head;
        head.next = deleteDuplicates(head.next);
        return head.val == head.next.val ? head.next : head;
    }
}

你可能感兴趣的:(LeetCode)