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;
}
相关推荐
superman超哥3 分钟前
Rust 所有权转移在函数调用中的表现:编译期保证的零成本抽象
开发语言·后端·rust·函数调用·零成本抽象·rust所有权转移
xiaowu0803 分钟前
C# 把dll分别放在指定的文件夹的方法
开发语言·c#
cchjyq10 分钟前
嵌入式按键调参:简洁接口轻松调参(ADC FLASH 按键 屏幕参数显示)
c语言·c++·单片机·mcu·开源·开源软件
mg66811 分钟前
0基础开发学习python工具_____用 Python + Pygame 打造绚丽烟花秀 轻松上手体验
开发语言·python·学习·pygame
无限进步_11 分钟前
【C语言】堆(Heap)的数据结构与实现:从构建到应用
c语言·数据结构·c++·后端·其他·算法·visual studio
xyd陈宇阳13 分钟前
C 语言宏定义(#define)语法与用法大全
c语言·嵌入式硬件
黎雁·泠崖21 分钟前
【线性表系列入门篇】从顺序表到链表:解锁数据结构的进化密码
c语言·数据结构·链表
CodeOfCC22 分钟前
C++ 实现ffmpeg解析hls fmp4 EXT-X-DISCONTINUITY并支持定位
开发语言·c++·ffmpeg·音视频
ghie909022 分钟前
基于LSB匹配的隐写术MATLAB实现
开发语言·计算机视觉·matlab
Lhan.zzZ24 分钟前
Qt绘制残留问题排查与修复日志
开发语言·数据库·qt