82:删除排序链表中的重复元素 II
This commit is contained in:
parent
dacd294624
commit
415fa76ce6
@ -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)
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p>
|
||||
|
||||
<p>返回同样按升序排列的结果链表。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg" style="width: 500px; height: 142px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,2,3,3,4,4,5]
|
||||
<strong>输出:</strong>[1,2,5]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg" style="width: 500px; height: 205px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,1,1,2,3]
|
||||
<strong>输出:</strong>[2,3]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>链表中节点数目在范围 <code>[0, 300]</code> 内</li>
|
||||
<li><code>-100 <= Node.val <= 100</code></li>
|
||||
<li>题目数据保证链表已经按升序排列</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 628</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user