Advertisement
Guest User

83. Remove Duplicates from Sorted List

a guest
Dec 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. class Solution {
  2.     public ListNode deleteDuplicates(ListNode head) {
  3.         if (head == null) {
  4.             return head;
  5.         }
  6.         ListNode current = head;
  7.         while (current != null) {
  8.             ListNode next = findNext(current);
  9.             current.next = next;
  10.             current = next;
  11.         }
  12.         return head;
  13.     }
  14.    
  15.     private static ListNode findNext(ListNode node) {
  16.         ListNode next = node.next;
  17.         while (next != null && node.val == next.val) {
  18.             next = next.next;
  19.         }
  20.         return next;
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement