Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null) { return head; } ListNode temp = new ListNode(0); temp.next = head; ListNode pre = temp; ListNode cur = head; ListNode nex = head.next; boolean flag = false; while (nex != null) { if (cur.val == nex.val) { flag = true; nex = nex.next; if (nex == null) { pre.next = null; } } else { if (flag) { pre.next = nex; flag = false; } else { pre = cur; } cur = nex; nex = nex.next; } } return temp.next; } }
相关推荐
Remove Duplicates from Sorted List II"是一个中等难度的链表处理问题,要求从已排序的链表中删除所有重复的元素,使得每个元素只出现一次。输入是一个单链表,其中节点值是整数,链表已经按升序排序。 【解法一...
javascript js_leetcode题解之82-remove-duplicates-from-sorted-list-ii.js
c c语言_leetcode题解之0082_remove_duplicates_from_sorted_list_ii.zip
python python_leetcode题解之083_Remove_Duplicates_from_Sorted_List
javascript js_leetcode题解之83-remove-duplicates-from-sorted-list.js
c c语言_leetcode题解之0083_remove_duplicates_from_sorted_list.zip
c语言入门 C语言_leetcode题解之83-remove-duplicates-from-sorted-list.c
- **2.2.5 Remove Duplicates from Sorted List II** - 移除排序链表中的重复元素,包括头部。 - 实现思路:使用虚拟头结点简化边界条件处理。 - **2.2.6 Rotate List** - 旋转链表。 - 实现思路:先找到链表...
Remove Duplicates from Sorted List II题目: | 源码:标签:单向链表难度:中等 / Medium146. LRU Cache题目: | 源码:标签:哈希表,双向链表难度:中等 / Medium212. Word-Search-II题目: | 英文站源码:./...
II(Remove Duplicates from Sorted List II) 2018.9.27 重建二叉树(Rebuild Binary Tree) 2018.9.28 把字符串转换成整数(Convert a string to an integer) 2018.10.8 树的子结构(Substructure of the tree) ...
**1.6 Remove Duplicates from Sorted List II (82)** - **问题描述**:给定一个已排序的链表,删除其中所有重复的元素,使得每个元素只出现一次。 - **解题思路**: - 使用虚拟头节点帮助处理边界情况。 - 遍历...
26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...
如 `@{listnew} Remove Duplicates ${list}` 创建一个没有重复元素的新列表,并通过 `List Should Not Contain Duplicates ${listnew}` 检查新列表是否不包含重复项。 5. **List Should Contain Sub List**: 检查一...
Duplicates from Sorted List 141 Easy Linked List Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List
Duplicates from Sorted List #0118 - Pascal's Triangle #0121 - Best Time to Buy and Sell Stock #0125 - Valid Palindrome #0136 - Single Number #0167 - Two Sum - Input Array is sorted #0189 - Rotate ...
83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 ...
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack ...Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack ...Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke
26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141....