当前位置: 代码网 > it编程>编程语言>正则表达式 > Leetcode算法题(链表的中间节点+返回倒数第k个节点+合并两个有序链表)

Leetcode算法题(链表的中间节点+返回倒数第k个节点+合并两个有序链表)

2024年08月01日 正则表达式 我要评论
本题力扣链接:https://leetcode.cn/problems/middle-of-the-linked-list/solutions/164351/lian-biao-de-zhong-jian-jie-dian-by-leetcode-solut/

题目1:

本题力扣链接:https://leetcode.cn/problems/middle-of-the-linked-list/solutions/164351/lian-biao-de-zhong-jian-jie-dian-by-leetcode-solut/

思路1:单指针法

首先我们对链表进行遍历,记录链表的总长度n,再进行遍历原链表,返回刚跳出循环大于n/2时,对应的链表节点,即为中间节点。

typedef struct listnode listnode;
struct listnode* middlenode(struct listnode* head) {
    listnode* pcur=head;
    if(head==null)
    {
        return head;
    }
    int count=0;
    while(pcur)
    {
        count++;
        pcur=pcur->next;
    }
    pcur=head;
    int k=0;
    while(k<count/2)
    {
        k++;
        pcur=pcur->next;
    }
    return pcur;
}

思路2:快慢指针法

typedef struct listnode listnode ;
struct listnode* middlenode(struct listnode* head) {
        listnode* slow = head;
        listnode* fast = head;
        while (fast != null && fast->next != null) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

场景1:

场景二:

题目2:

oj链接:

思路1:反转链表+遍历

先将原链表进行翻转,再将反转后的链表的头结点移动k-1位,最后直接返回此时节点的值。

typedef struct listnode listnode;
int kthtolast(struct listnode* head, int k){
    //1. 先将原链表进行翻转
    //2. 再将反转后的链表的头结点移动k-1位
    listnode* n1,*n2,*n3;
    n1=null,n2=head,n3=n2->next;
    while(n2)
    {
        n2->next=n1;
        n1=n2;
        n2=n3;
        if(n3)
        n3=n3->next;
    }
    k=k-1;
    while(k--)
    {
        n1=n1->next;
    }
    return n1->val;
}

复杂度分析:

时间复杂度:o(n),其中n为给定链表节点的数目。

空间复杂度:o(1)

思路2:遍历+挪动

先计算原链表的总长度s,再将原链表的头结点移动s-k位,返回此时节点对应的值。

typedef struct listnode listnode;
int kthtolast(struct listnode* head, int k){
    listnode* pcur=head;
    int s=0;
    while(pcur)
    {
        s++;
        pcur=pcur->next;
    }
    int m=s-k;
    while(m--)
    {
        head=head->next;
    }
    return head->val;

}

复杂度分析:

时间复杂度:o(n),其中n为给定链表节点的数目。

空间复杂度:o(1)

题目3:

思路:遍历+尾插+比较

创建新的带头链表,依次遍历两个原链表,比较对应的值,尾插到新的链表尾部。

typedef struct listnode listnode;
struct listnode* mergetwolists(struct listnode* list1, struct listnode* list2) {
    if (list1 == null) {
        return list2;
    }
    if (list2 == null) {
        return list1;
    }
    listnode* newlist,*newhead;
     newhead=newlist=(listnode*)malloc(sizeof(listnode));
    while (list1 && list2) {
        if (list1->val > list2->val) {
            newlist->next = list2;
            list2 = list2->next;
        } else {
            newlist->next = list1;
            list1 = list1->next;
        }
        newlist = newlist->next;
    }
    if (list1)
        newlist->next = list1;
    if (list2)
        newlist->next = list2;
    return newhead->next;
}

如果本文章对你有帮助,期待你的三连!!!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com