C语言刷题 LeetCode 30天挑战 (八)快慢指针法

//Middle of the Linked List

//Given a non-empty, singly linked list with head node head ,

//return a middle node of linked list.

//If there are two middle nodes, return the second middle node

//Example 1:

//Input:1,2,3,4,5]

//0utput:Node 3from this list(Serialization:[3,4,5])

//The returned node has value 3, (The judge's serialization of this node is [3,4,5]).

//Note that we returned a ListNode object ans, such that:

//ans,val =3,ans.next,val =4,ans.next,next.val = 5, and ans.next.next.next = NULL.

//Example 2:

//Input:I[1,2,3,4,5,6]

//Output:Node 4 from this list(serialization:[4,5,6])

//Since the list has two middle nodes with values 3 and 4, we return the second one.

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

// Definition for singly-linked list.
struct ListNode {
    int val;
    struct ListNode *next;
};

// Function to find the middle node
struct ListNode* middleNode(struct ListNode *head) {
    struct ListNode *slow = head;
    struct ListNode *fast = head;

    // Move slow pointer one step and fast pointer two steps
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
    }
    
    // When fast reaches the end, slow is at the middle
    return slow;
}

// Helper function to create a new ListNode
struct ListNode* createNode(int val) {
    struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode));
    newNode->val = val;
    newNode->next = NULL;
    return newNode;
}

// Helper function to print the linked list from a given node
void printList(struct ListNode* node) {
    while (node != NULL) {
        printf("%d ", node->val);
        node = node->next;
    }
    printf("\n");
}

int main() {
    // Example 1: [1, 2, 3, 4, 5]
    struct ListNode* head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);
    head->next->next->next = createNode(4);
    head->next->next->next->next = createNode(5);
    
    struct ListNode* middle = middleNode(head);
    printf("Middle node value: %d\n", middle->val);
    printf("List from middle node: ");
    printList(middle);
    
    // Example 2: [1, 2, 3, 4, 5, 6]
    struct ListNode* head2 = createNode(1);
    head2->next = createNode(2);
    head2->next->next = createNode(3);
    head2->next->next->next = createNode(4);
    head2->next->next->next->next = createNode(5);
    head2->next->next->next->next->next = createNode(6);
    
    middle = middleNode(head2);
    printf("Middle node value: %d\n", middle->val);
    printf("List from middle node: ");
    printList(middle);

    return 0;
}
相关推荐
小白狮ww9 分钟前
RStudio 教程:以抑郁量表测评数据分析为例
人工智能·算法·机器学习
AAA修煤气灶刘哥11 分钟前
接口又被冲崩了?Sentinel 这 4 种限流算法,帮你守住后端『流量安全阀』
后端·算法·spring cloud
至此流年莫相忘12 分钟前
设计模式:模板方法模式
java·开发语言·设计模式
tan180°43 分钟前
Boost搜索引擎 查找并去重(3)
linux·c++·后端·搜索引擎
土了个豆子的1 小时前
02.继承MonoBehaviour的单例模式基类
开发语言·visualstudio·单例模式·c#·里氏替换原则
qq_172805591 小时前
Go 自建库的使用教程与测试
开发语言·后端·golang
yongui478341 小时前
CentOS系统如何查看当前内存容量
linux·运维·centos
Boop_wu1 小时前
[数据结构] 栈 · Stack
数据结构
久绊A1 小时前
Hydra-SSH 破解安全防范
开发语言·php
kk”1 小时前
C语言快速排序
数据结构·算法·排序算法