<LeetCode>双指针题目·

1.82. Remove Duplicates from Sorted List II (Medium)

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:
Input: 1->1->1->2->3
Output: 2->3

题目分析:

  1. Example 1中,要将重复的3全部删除,可以使用一个指针slow指向2,另外一个指针fast指向3,让2的next指向3的next
  2. 在Example 2中,看到需要删除的可能是头节点,因此需要一个dummy节点指向头节点,这样可以简单的统一的处理包括头节点在内的所有节点
    根据上述分析,如何找到fast的位置,fast要找到与下一个元素不同的。
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(-1); 取一个临时的节点指向head,方便后面删除head时候的处理
        dummy.next = head;
        // 找到删除的起点和终点
        ListNode slow = dummy;
        ListNode fast = dummy.next;
        while (fast != null) {
            // 找到下一个fast的位置,关键是fast.next.val != fast.val 同时注意,因为使用了fast.val 和fast.next.val 因此 fast 和 fast.next都不能为null
            while (fast != null && fast.next != null && fast.next.val == fast.val) {
                fast = fast.next;
            }
            if (slow.next != fast) { // fast和slow不相邻,说明中间有重复的元素
                slow.next = fast.next;
                fast = fast.next;
            } else { // fast和slow相邻,说明中间没有重复元素
                slow = slow.next;
                fast = fast.next;
            }
        }
        return dummy.next;
    }
}

原文地址:https://www.cnblogs.com/isguoqiang/p/11532255.html

时间: 2024-08-30 13:42:15

<LeetCode>双指针题目·的相关文章

LeetCode SQL题目(第一弹)

LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才能使用,我没有使用SSMS 或Workbench,而是直接使用sqlcmd,解决登陆问题可以参考这个链接(http://www.cnblogs.com/skynothing/archive/2010/08/26/1809125.html)感谢这位博主的帮助. 181. Employees Earni

LeetCode 十月份题目汇总

开源地址:点击该链接 前言 十月份共有60道题目,全部属于 Easy 难度的,所以公众号中分享出来的并不多,只是挑了一些感觉还可以的才分享了出来,这60道题目我按照不同类别进行了分类整理,所有源码以及对应的解题思路均匀开源到 GitHub,公众号内回复"LeetCode"获取,具体题目如下. 题目分类 数组 0001_two_sum 0026_remove_duplicates_from_sorted_array 0027_remove_element 0035_search_inse

怒刷leetcode的题目(1)237、104、136、100

https://leetcode.com/problemset/algorithms/上面的题目,每天做几道题目,大体从准确率高至低做下去 编程语言为c语言,因为跑的最快- 237 Delete Node in a Linked List 47.8% Easy Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed t

leetcode database题目

LeetCode有10道SQL的题目,最近学习SQL语言,顺便刷题强化一下, 说实话刷完SQL学习指南这本书,不是很难,上面的例子 跟语法规则我都能理解透, 实际中来做一些比较难的业务逻辑题,却一下子很难想起如何利用SQL来描述 还是要多刷题开阔自己的思路,把学到手的语法加以练习 应用到实际中去 197. Rising Temperature Given a Weather table, write a SQL query to find all dates' Ids with higher t

leetcode ---双指针+滑动窗口

一:Minimum Size Subarray Sum 题目: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, th

leetcode ---双指针+滑动窗体

一:Minimum Size Subarray Sum(最小长度子数组的和O(N)) 题目: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3

已知前序(后序)遍历序列和中序遍历序列构建二叉树(Leetcode相关题目)

1.文字描述: 已知一颗二叉树的前序(后序)遍历序列和中序遍历序列,如何构建这棵二叉树? 以前序为例子: 前序遍历序列:ABCDEF 中序遍历序列:CBDAEF 前序遍历先访问根节点,因此前序遍历序列的第一个字母肯定就是根节点,即A是根节点:然后,由于中序遍历先访问左子树,再访问根节点,最后访问右子树,所以我们找到中序遍历中A的位置,然后A左边的字母就是左子树了,也就是CBD是根节点的左子树:同样的,得到EF为根节点的右子树. 将前序遍历序列分成BCD和EF,分别对左子树和右子树应用同样的方法,

LeetCode Animation 题目图解汇总(持续更新中...)

我会尽力将LeetCode上所有的题目都用动画的形式演示出来,期待与你见证这一天! GitHub Repo:LeetCode Animation Follow: MisterBooo · GitHub Problems ID Problem Article Animation 001 两数之和 每天一算:Two Sum 019 删除链表的倒数第N个节点 每天一算:Remove Nth Node From End of List 020 有效的括号 每天一算:Valid Parentheses 0

[leetcode]算法题目 - Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod