- 浏览: 183486 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
在一个有序的链表中删除重复的元素,使每个元素只能出现一次。做链表删除节点的问题,我们一般采用一个辅助节点helper来记录头结点,用于返回。对于这道题目,头结点肯定不会删除,因此我们直接将helper节点指向头结点即可。如果head.val = head.next.val 就让head.next = head.next.next,这样就跳过了值相同的节点;否则head往前移动,head = head.next,知道遍历完所有的元素。代码如下:
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
在一个有序的链表中删除重复的元素,使每个元素只能出现一次。做链表删除节点的问题,我们一般采用一个辅助节点helper来记录头结点,用于返回。对于这道题目,头结点肯定不会删除,因此我们直接将helper节点指向头结点即可。如果head.val = head.next.val 就让head.next = head.next.next,这样就跳过了值相同的节点;否则head往前移动,head = head.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 = head; while(helper.next != null) { if(helper.next.val == helper.val) helper.next = helper.next.next; else helper = helper.next; } return head; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
Remove Duplicates from Sorted List II"是一个中等难度的链表处理问题,要求从已排序的链表中删除所有重复的元素,使得每个元素只出现一次。输入是一个单链表,其中节点值是整数,链表已经按升序排序。 【解法一...
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
javascript js_leetcode题解之82-remove-duplicates-from-sorted-list-ii.js
c c语言_leetcode题解之0082_remove_duplicates_from_sorted_list_ii.zip
- **2.2.5 Remove Duplicates from Sorted List II** - 移除排序链表中的重复元素,包括头部。 - 实现思路:使用虚拟头结点简化边界条件处理。 - **2.2.6 Rotate 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
- **Remove Duplicates from Sorted List**:从已排序的链表中移除重复项。 - **Merge Sorted Lists**:合并两个已排序的链表。 - **Reverse Linked List**:反转链表。 - **Swap Nodes in Pairs**:交换链表中...
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 ...
**1.6 Remove Duplicates from Sorted List II (82)** - **问题描述**:给定一个已排序的链表,删除其中所有重复的元素,使得每个元素只出现一次。 - **解题思路**: - 使用虚拟头节点帮助处理边界情况。 - 遍历...
83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 ...
Remove Duplicates from Sorted List II题目: | 源码:标签:单向链表难度:中等 / Medium146. LRU Cache题目: | 源码:标签:哈希表,双向链表难度:中等 / Medium212. Word-Search-II题目: | 英文站源码:./...
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
* Remove Duplicates from Sorted List:给定一个已排序的链表,移除重复元素,并返回新链表。这个题目需要使用快慢指针的思想,将链表分解成更小的子链表,并移除重复元素。 8. 数学 数学是一种非常重要的知识...
Duplicates from Sorted List Palindrome Linked List LL中的插入排序 使用额外的缓冲区从未排序的链表中删除重复项 细绳 确定字符串是否包含所有唯一字符 (CTCI) 在不使用额外缓冲区的情况下删除字符串中的重复字符...
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) ...
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...