关于链表的经典OJ题

表锅,我出来了哦!!!

表锅,表集,表die,表美 们,看我金天又给大噶带来了什么咧???

------------额,金天给大家伙带来了,关于链表的三大经典OJ题哦!(哈哈哈哈哈哈)


1.反转单链表:https://leetcode-cn.com/problems/reverse-linked-list/

2.链表的中间结点:https://leetcode-cn.com/problems/middle-of-the-linked-list/

3.合并两个有序链表:https://leetcode-cn.com/problems/merge-two-sorted-lists/

注:这些题都在来自力扣,大家可以复制链接直接进去答题哦!!!


 接下来,让我们进入今天的学习吧!!!


1.反转单链表:请你反转链表,并返回反转后的链表。

例图:

 思路一:

 疑问1:为什么不是n3==NULL停止循环了???

 疑问2:要是链表为空了

答:要是链表为空了,直接返回NULL

注:重复的过程一般是用循环解决

循环三要素:初识值,迭代过程,结束条件

思路一代码实现:

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


struct ListNode* reverseList(struct ListNode* head){
    if(head==NULL)
    return NULL;
    //初始化
    struct ListNode* n1 = NULL,*n2 = head,*n3 = n2->next;
    //结束条件
    while(n2)
    {
        //迭代过程(重复过程)
        n2->next = n1;
        n1 = n2;
        n2 = n3;
        if(n3)
        n3 = n3->next;
    }
    return n1;
}

​

思路二:头插法

 思路二代码实现:

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


struct ListNode* reverseList(struct ListNode* head){
    if(head==NULL)
    {
        return NULL;
    }
   struct ListNode *cur = head,*next = cur->next;
   struct ListNode* newhead = NULL;
   while(cur)
   {
       cur->next = newhead;
       newhead = cur;
       cur = next;
       if(next)
       next = next->next;
   }
   return newhead;
}

2.链表的中间结点:

给定一个头结点为head的非空单链表,返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点。

例1:输入【1,2,3,4,5】  输出:3

例2:输入【3,4,5,6】   输出:5

思路:

 大家从上面的思路中也可以看出它也是迭代的过程,初始条件是slow,fast都指向第一个结点,迭代过程,slow走一步,fast走两步,结束条件fast->next,fast只要有一个为空就结束。

代码:

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


struct ListNode* middleNode(struct ListNode* head){
    struct ListNode *slow = head,*fast = head;
    while(fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

3.合并两个有序链表:将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

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


struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    if(list1==NULL)
    {
        return list2; 
    }
    if(list2==NULL)
    {
        return list1;
    }
    struct ListNode *head = NULL,*tail = NULL;
    while(list1 != NULL && list2 != NULL)
    {
        if(list1->val < list2->val)
        {
            if(tail == NULL)
            {
                head = tail = list1;
            }
            else
            {
                tail->next = list1;
                tail = tail->next;
            }
            list1=list1->next;
        }
        else
        {
            if(tail == NULL)
            {
                head = tail = list2;
            }
            else
            {
                tail->next = list2;
                tail=tail->next;
            }
            list2=list2->next;
        }
    }
    if(list1)
    {
        tail->next = list1;
    }
    if(list2)
    {
        tail->next = list2;
    }

    return head;
}

 表锅,表集,表die,表美 们今天就到这里了哦!!!

  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

拼命阿紫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值