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;
}
相关推荐
极客小张7 分钟前
基于STM32的智能家居语音控制系统:集成LD3320、ESP8266设计流程
c语言·stm32·物联网·算法·毕业设计·课程设计·语言识别
Tech_gis19 分钟前
C++ 观察者模式
开发语言·c++·观察者模式
卑微求AC20 分钟前
继电器原理及应用
c语言·开发语言·51单片机·嵌入式
曳渔26 分钟前
Java-数据结构-反射、枚举 |ू・ω・` )
java·开发语言·数据结构·算法
laocooon52385788627 分钟前
java 模拟多人聊天室,服务器与客户机
java·开发语言
风槐啊28 分钟前
六、Java 基础语法(下)
android·java·开发语言
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂38 分钟前
算法与程序课程设计——观光铁路
c语言·c++·算法·课程设计·dijkstra 算法·spfa算法
网安老伯1 小时前
【2024版】最新kali linux入门及常用简单工具介绍(非常详细)零基础入门到精通,收藏这一篇就够了_kalilinux
linux·运维·服务器·开发语言·web安全·网络安全·xss
laocooon5238578861 小时前
java类的混搭,
java·开发语言
忘梓.1 小时前
C嘎嘎入门篇:类和对象番外(时间类)
c++·算法