- 浏览: 184786 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
与Remove Duplicates from Sorted List相比,这道题目要求删除所有重复的元素,这样头结点可能会被删除,我们这时同样需要创建一个辅助节点,但是这次不是直接用辅助节点helper记录头结点,而是将helper.next = head,因为head可能会被删除,因此我们要返回的节点要根据helper.next来确定。这样相当于在原有链表的头上多加了一个节点,让head = helper,从head的开始操作,比较head.next的值与head.next.next的值,如果相等,用一个while循环来删除所有重复的元素;如果不等让head后移。最后返回helper.next。代码如下:
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
与Remove Duplicates from Sorted List相比,这道题目要求删除所有重复的元素,这样头结点可能会被删除,我们这时同样需要创建一个辅助节点,但是这次不是直接用辅助节点helper记录头结点,而是将helper.next = head,因为head可能会被删除,因此我们要返回的节点要根据helper.next来确定。这样相当于在原有链表的头上多加了一个节点,让head = helper,从head的开始操作,比较head.next的值与head.next.next的值,如果相等,用一个while循环来删除所有重复的元素;如果不等让head后移。最后返回helper.next。代码如下:
/** * 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 helper = new ListNode(0); helper.next = head; head = helper; while(head != null && head.next != null && head.next.next != null) { if(head.next.val == head.next.next.val) { int value = head.next.val; while(head.next != null && head.next.val == value) { head.next = head.next.next; } } else { head = head.next; } } return helper.next; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 269Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 388Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 378Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 482Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 666Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 433Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 582Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 933Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ...
相关推荐
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....