算法复习:双指针(对撞指针、快慢指针)

一、快慢指针: leedcode 142. 环形链表 II

快慢指针的思想是设置慢指针slow和快指针fast,slow每次走一步,fast每次走两步,如果有环fast指针和slow指针必然相遇,相遇时

定义新的指针p从head开始和slow从当前位置起每次都走一步,直到相遇,相遇的位置就是环的入口。

class Solution {
public:
    ListNode *detectCycle(ListNode *head)
    {
        int lable=0;
        struct ListNode *slow,*fast,*pp;
        if(head==NULL)
            return NULL;
        if(head->next==NULL)
            return NULL;
        slow=head->next;
        if(slow->next==NULL)
            return NULL;
        fast=slow->next;
        while(slow!=fast)// 步骤一:使用快慢指针判断链表是否有环
        {
            if(fast->next==NULL)
            {
                lable=1;
                break;
            }
            fast=fast->next;//快指针走两步
            if(fast->next==NULL)
            {
                lable=1;
                break;
            }
            fast=fast->next;
            slow=slow->next;//慢指针走一步
        }
        if(lable==1)
            return NULL;
        pp=head;
        while(pp!=slow)// 步骤二:若有环,找到入环开始的节点
        {
            pp=pp->next;
            slow=slow->next;
        }
        return pp;
    }
};

leedcode 142

原文地址:https://www.cnblogs.com/dzzy/p/12253675.html

时间: 2024-08-09 15:41:41

算法复习:双指针(对撞指针、快慢指针)的相关文章

一文学会链表快慢指针解题技巧

前言 上文 我们详细地学习了链表的基本概念,优缺点,也带大家一步步由浅入深地学习了链表的翻转技巧,这一篇我们来看看链表的另一个解题技巧:快慢指针. 快慢指针在面试中出现的概率也很大,也是务必要掌握的一个要点,本文总结了市面上常见的快慢指针解题技巧,相信看完后此类问题能手到擒来.本文将详细讲述如何用快慢指针解决以下两大类问题 寻找/删除第 K 个结点 有关链表环问题的相关解法 寻找/删除第 K 个结点 小试牛刀之一 LeetCode 876:给定一个带有头结点 head 的非空单链表,返回链表的中

快慢指针原理--快速找到未知长度单链表的中间节点

package com.java.dataStruct; //节点类 public class Node<E> { E item; Node next; public Node(){ } public Node(E element){ this.item = element; } public Node(E element, Node next){ this.item = element; this.next = next; } } Node p1,r1; Node L1 = new Node

快慢指针

//最快的方式查找一个链表中的最中间节点#include<stdio.h> #define SIZE 100000 typedef struct Node{ int data; struct Node* next; struct Node* prev; }Node; Node f[SIZE]; int index = 0; Node* head; Node* getnode(){ return &f[index++]; } void init(){ index = 0; head =

使用哈希算法将数字解析为函数指针-一种架构方法

使用哈希算法将数字解析为函数指针: 这也算是最简单的,不会带有地址冲突的哈希了,哈希函数可以描述为: func = arr[index].func index为输入,根据输入的index,找到其对应的函数指针返回 这种架构虽然简单,但是在做测试时还是非常有用的 比如一种测试有几十项,我可以使用这种架构来实现自动轮巡测试,或者手动交互时输入一个Index,即可以去调用对应的测试函数 另外根据这个代码,还可以学习到函数指针的定义和使用: 定义:typedef int (*FuncPtr)(char

求单链表的中间节点,用快慢指针

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Node* findMid(Node* &head) {     if(head == NULL||head->next == NULL)         return head;          Node* p = head;     Node* q = head;     while(q->next->next&&(q = q->next))     {         p = p-

reorder-list——链表、快慢指针、逆转链表、链表合并

Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example,Given{1,2,3,4}, reorder it to{1,4,2,3}. 由于链表尾端不干净,导致fast->next!=NULL&&fast->next-&

快慢指针到底指向哪?

快慢指针到底指向哪?这个问题对于刚接触这种技巧的人来说往往把握不好.下面将以单链表为例, 说一点我个人的理解. 1) 比较通用的写法 if (head == NULL || head->next == NULL) return true; ListNode *fast = head, *slow = head, *prev = NULL; while (fast && fast->next) { prev = slow; slow = slow->next; fast =

[LeetCode] Remove Nth Node From End of List 快慢指针

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

Minimum Window Substring &amp;&amp;&amp; Longest Substring Without Repeating Characters 快慢指针,都不会退,用hashmap或者其他结构保证

1 public class Solution { 2 public static int lengthOfLongestSubstring(String s) { 3 4 char[] arr = s.toCharArray(); 5 int pre = 0; 6 7 HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 8 9 for (int i = 0; i < arr.length;