From 415fa76ce64015901ba107361acc762c5a58ad47 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 7 Jun 2021 10:24:39 +0800 Subject: [PATCH] =?UTF-8?q?82:=E5=88=A0=E9=99=A4=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E9=87=8D=E5=A4=8D=E5=85=83?= =?UTF-8?q?=E7=B4=A0=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/RemoveDuplicatesFromSortedListIi.java | 87 +++++++++++++++++++ .../cn/RemoveDuplicatesFromSortedListIi.md | 30 +++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedListIi.java create mode 100644 src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedListIi.md diff --git a/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedListIi.java b/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedListIi.java new file mode 100644 index 0000000..69bcd61 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedListIi.java @@ -0,0 +1,87 @@ +//存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。 +// +// 返回同样按升序排列的结果链表。 +// +// +// +// 示例 1: +// +// +//输入:head = [1,2,3,3,4,4,5] +//输出:[1,2,5] +// +// +// 示例 2: +// +// +//输入:head = [1,1,1,2,3] +//输出:[2,3] +// +// +// +// +// 提示: +// +// +// 链表中节点数目在范围 [0, 300] 内 +// -100 <= Node.val <= 100 +// 题目数据保证链表已经按升序排列 +// +// Related Topics 链表 +// 👍 628 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +//82:删除排序链表中的重复元素 II +public class RemoveDuplicatesFromSortedListIi{ + public static void main(String[] args) { + //测试代码 + Solution solution = new RemoveDuplicatesFromSortedListIi().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +/** + * 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) { + if (head == null || head.next == null) { + return head; + } + ListNode front = new ListNode(-1); + ListNode temp = front; + int same = head.val; + boolean flag = false; + while (head.next != null) { + if (head.val == head.next.val) { + head.next = head.next.next; + same = head.val; + flag = true; + } else if (flag && head.val == same) { + head = head.next; + } else { + temp.next = head; + temp = temp.next; + head = head.next; + } + } + if (head.val == same) { + temp.next = null; + } else { + temp.next = head; + } + return front.next; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedListIi.md b/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedListIi.md new file mode 100644 index 0000000..193f139 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedListIi.md @@ -0,0 +1,30 @@ +

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。

+ +

返回同样按升序排列的结果链表。

+ +

 

+ +

示例 1:

+ +
+输入:head = [1,2,3,3,4,4,5]
+输出:[1,2,5]
+
+ +

示例 2:

+ +
+输入:head = [1,1,1,2,3]
+输出:[2,3]
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 链表
  • \n
  • 👍 628
  • 👎 0
  • \ No newline at end of file