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

目录

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

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

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

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

 


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

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

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:反转链表

思路一:

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:链表的中间节点

思路一:计数法

/**
 * 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;
}

思路二:快慢指针

/**
 * 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个位置,于是逃过了这场死亡游戏。

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @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:分割链表

/**
 * 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;
}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西阳未落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值