[LC] 83 title Remove Duplicates from Sorted List (delete duplicate elements sorted linked list) (list)

① English title

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

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

Example 2:

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

② Chinese title

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

③ ideas

   Very simple, this is one time I write, it is passed.

   Because this problem is to delete duplicate elements, it appears only once, unlike the guy wins the offer in question asked to delete duplicate all deleted, leaving no one, so this question is simple.

   Directly traverse the current node curr.val elements comparable to the elements curr.next.val next node,

④ Code

. 1  class Solution {
 2      public ListNode deleteDuplicates (ListNode head) {
 . 3          IF (head == null || head.next == null )
 . 4              return head;
 . 5          ListNode Curr = head;
 . 6          the while (! Curr.next = null ) {     // because I guarantee the .next is not empty, then the low 8-line call .next.next I would not be out of range. 
. 7              IF (curr.val == curr.next.val)
 . 8                  curr.next = curr.next.next;
 . 9              the else 
10                  Curr = curr.next;
 . 11         }
12         return head;
13     }
14 }

⑤ submit results

    

Results of the:
by
When execution: 1 ms, beat the 99.86% of all users to submit in Java
Memory consumption: 36.6 MB, defeated 62.77% of all users to submit in Java

⑥ learned 

1, 3 and 4 of this line of logic or written, is dedicated writing, I've seen all the "guarantee list is not empty, or ensure that there is more than one element of the list" of writing, the most simple of. Learn about, after each encounter, are written. 
2, line 6 because I guarantee the .next is not empty, then the low 8-line call .next.next I would not be out of range.

Guess you like

Origin www.cnblogs.com/zf007/p/11607296.html