快慢指针

//最快的方式查找一个链表中的最中间节点#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 = getnode();
    head->prev = head;
    head->next = head;
    head->data = -1;
}
void InsertAfter(Node*dst,Node*newnode){
    newnode->prev = dst;
    newnode->next = dst->next;
    dst->next->prev = newnode;
    dst->next = newnode;
}

int main(){
    init();
    Node* temp = getnode();//head的地址要保存下来用于遍历
    for(int i=1;i<21;i++){
        Node* p = getnode();
        p->data = i;
        if(i==1){temp = head;}
        else{temp=temp->next;}
            InsertAfter(temp,p);
    }
    Node* p1,*p2;//快慢指针
    p1 = head->next;
    p2 = head->next;
    while(p1->data!=-1){
        p1 = p1->next->next;
        p2 = p2->next;
    }
    printf("%d\n",p2->prev->data);
    return 0;
}
时间: 2024-10-15 17:35:43

快慢指针的相关文章

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

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

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

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