Leetcode - Plus One Linked List

Leetcode - Remove Duplicates from Sorted List II

violet posted @ Aug 15, 2020 07:22:21 AM in 算法 with tags Algorithm Linked List java , 10720 阅读

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Return the linked list sorted as well.

Example 1:

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

Example 2:

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

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode prev = dummy;
        ListNode node = head;
        
        while (node != null) {
            while (node.next != null && node.val == node.next.val) {
                node = node.next;
            }
            if (prev.next == node) {
                prev = prev.next;
            } else {
                prev.next = node.next;
            }
            node = node.next;
        }
        return dummy.next;
    }
}
rasmika 说:
Sep 15, 2021 05:41:35 PM

I found this one pretty enthralling, and you should go into my get-together also.
<a href="https://www.callgirlsdehradunescort.com/haridwar-escorts.html">Haridwar Escorts</a>
<a href="https://www.callgirlsdehradunescort.com/roorkee-escorts.html">Escorts in Roorkee</a>
<a href="https://www.callgirlsdehradunescort.com/haldwani-escorts.html">Haldwani Escorts Service</a>
<a href="https://www.callgirlsdehradunescort.com/chennai-escorts.html">Escorts Service in Chennai</a>
<a href="https://www.callgirlsdehradunescort.com/rishikesh-escorts.html">Rishikesh Escort</a>
We give math homework help associations beginning with one side of the planet then onto the going with. I'm stunned by your article. We like that obligingly keep on making more substance.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter