快慢指针问题

问题概述:

  快速找到未知长度单链表的中间结点。

代码实现:

 1 Status GetMidNode(LinkList L, ElemType *e)
 2 {
 3     LinkList search, mid;
 4     mid = search = L;
 5
 6     while (search->next != NULL)
 7     {
 8         //search移动的速度是 mid 的2倍
 9         if (search->next->next != NULL)
10         {
11             search = search->next->next;
12             mid = mid->next;
13         }
14         else
15         {
16             search = search->next;
17         }
18     }
19
20     *e = mid->data;
21
22     return OK;
23 }
时间: 2024-08-25 07:46:17

快慢指针问题的相关文章

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

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 =

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

? 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;

【Leetcode解题报告】快慢指针

快慢指针中的快慢指的是移动的步长,即每次向前移动速度的快慢.例如可以让快指针每次沿链表向前移动2,慢指针每次向前移动1次. Leetcode 141 Linked List Cycle 问题描述 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 分析与解法 大家可以想一下上体育课长跑的情景.当同学们绕着操场跑步的时候,速度快的

快慢指针,怎样快速获取链表的中间元素

有一个链表,怎样快速获取中间节点的元素. 方法1 遍历一下链表,确认长度,获取中间的节点元素,时间复杂度O(N) 方法2 设置两个指针,一个遍历p->nexr  一个 p->next->next,快慢指针的思想  时间复杂度为O(n/2) 代码: 1 //腾讯面试题,获取一个单链表的中间位置 2 Status GetMidNode(LinkList L,ElemType *e) 3 { 4 LinklList search,mid; 5 mid =search=L; 6 while (s

快慢指针及应用【转】

快慢指针中的快慢指的是移动的步长,即每次向前移动速度的快慢.例如可以让快指针每次沿链表向前移动2,慢指针每次向前移动1次. 判断单链表是否为循环链表 让快慢指针从链表头开始遍历,快指针向前移动两个位置,慢指针向前移动一个位置;如果快指针到达NULL,说明链表以NULL为结尾,不是循环链表.如果 快指针追上慢指针,则表示出现了循环. fast=slow=head; fast=fast->next->next; slow=slow->next; while(true){ if (fast==