首页 > 其他分享 >【LeetCode】876_链表的中间结点_C

【LeetCode】876_链表的中间结点_C

时间:2024-03-01 16:24:22浏览次数:22  
标签:结点 ListNode struct 876 head 链表 LeetCode 指针

题目描述

给你单链表的头结点 head ,请你找出并返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点。

https://leetcode.cn/problems/middle-of-the-linked-list/description/

示例

提示:

  • 链表的结点数范围是 [1, 100]
  • 1 <= Node.val <= 100

解题思路

  • 思路一

遍历链表得到结点数,根据结点数求中间结点所在位置,再次遍历链表

  • 思路二

设置两个指针,一个指针为“快指针”,每次前进两个结点,一个指针为“慢指针”,每次前进一个结点,当快指针指向链表尾结点时,慢指针指向链表的中间结点

代码实现

  • 思路一的代码实现
struct ListNode* middleNode(struct ListNode* head) {
    int count = 0;
    struct ListNode* p = head;
    while (p != NULL)
    {
        count++;
        p = p->next;
    }
    int mid = count / 2 + 1;
    int i = 0;
    p = head;
    for(i = 0; i < mid - 1; i++)
    {
        p = p->next;
    }
    return p;
}
  • 思路二的代码实现
struct ListNode* middleNode(struct ListNode* head) {
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    while(fast != NULL && fast->next != NULL)	//当快指针和下个结点都不为空时,循环继续
    {
        fast = fast->next->next;
        slow = slow->next;
    }
    return slow;
}

标签:结点,ListNode,struct,876,head,链表,LeetCode,指针
From: https://www.cnblogs.com/changbaiqiusha/p/18047337

相关文章

  • 代码随想录算法训练营day10 | leetcode 232. 用栈实现队列、225. 用队列实现栈
    目录题目链接:232.用栈实现队列-简单题目链接:225.用队列实现栈-简单题目链接:232.用栈实现队列-简单题目描述:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):实现MyQueue类:voidpush(intx)将元素x推到队列的末尾intp......
  • LeetCode 2345. Finding the Number of Visible Mountains
    原题链接在这里:https://leetcode.com/problems/finding-the-number-of-visible-mountains/description/题目:Youaregivena 0-indexed 2Dintegerarray peaks where peaks[i]=[xi,yi] statesthatmountain i hasapeakatcoordinates (xi,yi).Amountaincan......
  • [LeetCode] 2864. Maximum Odd Binary Number
    Youaregivenabinarystringsthatcontainsatleastone'1'.Youhavetorearrangethebitsinsuchawaythattheresultingbinarynumberisthemaximumoddbinarynumberthatcanbecreatedfromthiscombination.Returnastringrepresentin......
  • 代码随想录算法训练营day09 | leetcode 28. 找出字符串中第一个匹配项的下标、459. 重
    目录题目链接:28.找出字符串中第一个匹配项的下标-简单题目链接:459.重复的子字符串-简单题目链接:28.找出字符串中第一个匹配项的下标-简单题目描述:给你两个字符串haystack和needle,请你在haystack字符串中找出needle字符串的第一个匹配项的下标(下标从0开始)。如果ne......
  • Leetcode刷题第十五天-链表
    203:移除链表元素链接:203.移除链表元素-力扣(LeetCode)#Definitionforsingly-linkedlist.#classListNode:#def__init__(self,val=0,next=None):#self.val=val#self.next=nextclassSolution:defremoveElements(self,head:Op......
  • 【leetcode】412_FizzBuzz_C
    题目描述给你一个整数n,找出从1到n各个整数的FizzBuzz表示,并用字符串数组answer(下标从1开始)返回结果,其中:answer[i]=="FizzBuzz"如果i同时是3和5的倍数。answer[i]=="Fizz"如果i是3的倍数。answer[i]=="Buzz"如果i是5的倍数。answer[i]==......
  • 【C++】相对于数组,在链表中添加和删除元素更容易,但排序速度更慢。这就引出了一种可能
    相对于数组,在链表中添加和删除元素更容易,但排序速度更慢。这就引出了一种可能性:相对于使用链表算法进行排序,将链表复制到数组中,对数组进行排序,再将排序后的结果复制到链表中的速度可能更快;但这也可能占用更多的内存。请使用如下方法检验上述假设。a.创建大型vector<int>对象vi0,并......
  • 代码随想录算法训练营day08 | leetcode 344. 反转字符串、541. 反转字符串 II、54. 替
    目录题目链接:344.反转字符串-简单题目链接:541.反转字符串II-简单题目链接:[54.替换数字](题目页面(kamacoder.com))题目链接:151.反转字符串中的单词-中等题目链接:[55.右旋字符串](题目页面(kamacoder.com))题目链接:344.反转字符串-简单题目描述:编写一个函数,其作用是将......
  • 142. 环形链表 II C
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*detectCycle(structListNode*head){if(!head||!head->next)returnNULL;structListNode*slow=head;s......
  • 面试题 02.07. 链表相交C
    利用链表的特性,如果相交的话,后面就不可能岔开!你可以想象把他们有同一个尾巴,然后从尾巴往前看。所以只要知道两个链表的长度,就可以在同一起跑线上一起比较了。/***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;......