zoukankan      html  css  js  c++  java
  • Leecode no.82 删除排序链表中的重复元素 II

    package leecode;

    /**
    * 82. 删除排序链表中的重复元素 II
    *
    * 存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。
    *
    * 返回同样按升序排列的结果链表。
    *
    * @author Tang
    * @date 2021/12/21
    */
    public class DeleteDuplicates2 {

    /**
    * 存一个value 保存的是上一个该删除的元素值
    *
    *
    * @param head
    * @return
    */
    public ListNode deleteDuplicates(ListNode head) {
    if(head == null) {
    return null;
    }

    ListNode first = new ListNode();
    first.next = head;
    int value = Integer.MIN_VALUE;

    ListNode temp = first;
    while (head.next != null) {
    if(head.val == head.next.val || head.val == value) {
    //记录下这个重复的值
    value = head.val;
    //删掉head
    temp.next = head.next;
    } else {
    temp = temp.next;
    }

    head = head.next;
    }

    if(head.val == value) {
    temp.next = null;
    }
    return first.next;
    }


    public static void main(String[] args) {

    }





    }
  • 相关阅读:
    c++ 01
    unix c 11
    unix c 10
    unix c 09
    unix c 08
    unix c 07
    unix c 06
    unix c 05
    unix c 04
    Petrozavodsk Summer Training Camp 2017
  • 原文地址:https://www.cnblogs.com/ttaall/p/15717255.html
Copyright © 2011-2022 走看看