Java implementation LeetCode 83 deleted sort the list of repeating elements

83. Delete the sort the list of repeating elements

Given a sorted list, delete all the duplicate elements, so that each element occurs only once.

Example 1:

Input: 1-> 1-> 2
Output: 1-> 2
Example 2:

Input: 1-> 1-> 2-> 3-> 3
Output: 1-> 2-> 3

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
      public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        head.next = deleteDuplicates(head.next);
        if(head.val == head.next.val) head = head.next;
        return head;
    }
}
Released 1197 original articles · won praise 10000 + · views 560 000 +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104349807