C语言(17)——单链表的应用

目录

1.单链表经典算法OJ题⽬

1.1单链表相关经典算法OJ题1:移除链表元素

1.2单链表相关经典算法OJ题2:反转链表

[1.3 单链表相关经典算法OJ题3:链表的中间节点](#1.3 单链表相关经典算法OJ题3:链表的中间节点)


1.单链表经典算法OJ题⽬

1.1单链表相关经典算法OJ题1:移除链表元素

cpp 复制代码
struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* newhead,* newtail;
    newtail=newhead=NULL;
    struct ListNode* pcur=head;
    while(pcur)
    {
        if(pcur->val!=val)
        {
            if(newhead==NULL)
            {
                newhead=newtail=pcur;
            }
            else{
                newtail->next=pcur;
                newtail=newtail->next;
            }
        }
        pcur=pcur->next;
    }
    if(newtail)
    newtail->next=NULL;
    return newhead;
}

1.2单链表相关经典算法OJ题2:反转链表

思路一:

cpp 复制代码
struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode* prev = NULL;
    struct ListNode* curr = head;
    while (curr) {
        struct ListNode* next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

思路二:(指针)

1.3 单链表相关经典算法OJ题3:链表的中间节点

思路一:计数法

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* middleNode(struct ListNode* head) {
    struct ListNode* p=head;
    struct ListNode* q=head;
    int k=0,j=0;
    while(p)
    {
        k++;
        p=p->next;
    }
    struct ListNode* phead=NULL;
    struct ListNode* ptail=NULL;
    while(q)
    {
        j++;
        if(j>=k/2+1)
        {
            if(phead==NULL)
            phead=ptail=q;
            else
            {
                phead->next=q;
                phead=phead->next;
            }
        }
        q=q->next;
    }
    if(phead) phead->next=NULL;
    return ptail;
}

思路二:快慢指针

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* middleNode(struct ListNode* head) {
    struct ListNode* slow=head;
    struct ListNode* fast=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
    }
    return slow;
}

1.4单链表相关经典算法OJ题4:合并两个有序链表

1.5 循环链表经典应⽤------环形链表的约瑟夫问题

著名的Josephus问题

据说著名犹太历史学家Josephus有过以下的故事:在罗⻢⼈占领乔塔帕特后,39个犹太⼈与 Josephus及他的朋友躲到⼀个洞中,39个犹太⼈决定宁愿死也不要被⼈抓到,于是决定了⼀个⾃杀 ⽅式,41个⼈排成⼀个圆圈,由第1个⼈开始报数,每报数到第3⼈该⼈就必须⾃杀,然后再由下⼀ 个重新报数,直到所有⼈都⾃杀⾝亡为⽌。

然⽽Josephus和他的朋友并不想遵从,Josephus要他的朋友先假装遵从,他将朋友与⾃⼰安排在 第16个与第31个位置,于是逃过了这场死亡游戏。

cpp 复制代码
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param n int整型 
 * @param m int整型 
 * @return int整型
 */
struct ListNode* buynode(int x)
{
    struct ListNode*newnode=(struct ListNode*)malloc(sizeof(struct ListNode));
    newnode->val=x;
    newnode->next=NULL;
    return newnode;
}
struct ListNode* create(int n)
{
    struct ListNode* phead=buynode(1);
    struct ListNode* ptail=phead;
    for(int i=2;i<=n;i++)
    {
        ptail->next=buynode(i);
        ptail=ptail->next;
    }
    ptail->next=phead;
    return ptail;
}
int ysf(int n, int m ) {
   struct ListNode*prev=create(n);
   struct ListNode*pcur=prev->next;
   int count=1;
   while(pcur->next!=pcur)
   {
    if(count==m)
    {
        prev->next=pcur->next;
        free(pcur);
        pcur=prev->next;
        count=1;
    }
    else {
    {
        prev=pcur;
        pcur=pcur->next;
        count++;
    }
    }
   }
   return pcur->val;
}

1.4单链表相关经典算法OJ题5:分割链表

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* partition(struct ListNode* head, int x){
    if(head==NULL)
    {
        return head;
    }
    struct ListNode* lesshead,*lesstail;
    struct ListNode* greatehead,*greatetail;
    lesshead=lesstail=(struct ListNode*)malloc(sizeof(struct ListNode));
    greatehead=greatetail=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*pcur=head;
    while(pcur)
    {
        if(pcur->val<x)
        {
            lesstail->next=pcur;
            lesstail=lesstail->next;
        }
        else{
            greatetail->next=pcur;
            greatetail=greatetail->next;
        }
        pcur=pcur->next;
    }
    greatetail->next=NULL;
    lesstail->next=greatehead->next;
    return lesshead->next;
}
相关推荐
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔3 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔3 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing102329293 天前
Day03_刷题niuke20250915
c语言
我是菜鸟0713号3 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_3 天前
QT(4)
开发语言·汇编·c++·qt·算法