链表分割、删除重复结点、回文、相交链表、环形链表OJ

这篇博客介绍了四种链表操作:以指定值x分割链表,删除排序链表中的重复结点,判断链表是否为回文结构,以及找出两个链表的相交节点。提供了相关链接并讨论了时间复杂度和空间复杂度的优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链表分割:
链接: link.
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        // write code here
        if(!pHead)
        {
            return pHead;
        }
        ListNode* p = new ListNode(-1);
       ListNode* q = new ListNode(-1);
        ListNode* ph = p;
        ListNode* qh= q;
        while(pHead)
        {
            if(pHead->val < x)
            {
                p->next = pHead;
                p = p->next;
            }
            else 
            {
                q->next = pHead;
                q = q->next;
            }
            pHead = pHead->next;
        }
        q->next = nullptr;
        qh = qh->next;
        p->next = qh;
        return ph->next;
    }
};

删除链表中的重复结点
链接: link.
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        ListNode* newhead = new ListNode(-1);
        newhead->next = pHead;
        ListNode* prev = newhead;
        ListNode* cur = pHead;
        ListNode* next;
            while(cur&&cur->next)
            {
                next = cur->next;
                if(cur->val == next->val)
                {
                    while(next!=NULL && cur->val == next->val)
                    {
                        next = next->next;
                    }
                        prev->next = next;
                        cur= next;
                    
                }
                else
                {
                    prev = cur;
                    cur = cur->next;
                }
            }
        return newhead->next;
    }
};

链表的回文结构
链接: link.
对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。
给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。
测试样例:
1->2->2->1
返回:true

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:
    bool chkPalindrome(ListNode* A) {
        // write code here
        ListNode* fast = A;
        ListNode* slow = A;
        while(fast != NULL && fast->next !=NULL)
        {
            fast = fast->next->next;
            slow = slow->next;
        }
        ListNode* mid = slow;
        ListNode* midnext = slow->next;
        slow->next = NULL;
        ListNode* prev = NULL;
        ListNode* next = NULL;
        while(midnext)
        {
            next = midnext->next;
            midnext->next = prev;
            prev = midnext;
            midnext = next;
        }
        while(A!=NULL && prev != NULL)
        {
            if(A->val != prev->val)
            {
                return false;
            }
            A = A->next;
            prev = prev->next;
        }
        return true;
    }

相交链表
链接: link.
编写一个程序,找到两个单链表相交的起始节点。
如下面的两个链表:
在节点 c1 开始相交。
在这里插入图片描述
示例 1:
在这里插入图片描述
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
示例 2:
在这里插入图片描述
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
示例 3:
在这里插入图片描述
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。
注意:

如果两个链表没有交点,返回 null.
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode* fast = headA;
    struct ListNode* slow = headB;
    struct ListNode* tmp = NULL;
    if(headA == NULL&& headB == NULL)
        return NULL;
    int count1 = 0;
    int count2 = 0;
    while(fast)
    {
        fast = fast->next;
        count1++;
    }
    while(slow)
    {
        slow = slow->next;
        count2++;
    }
    int step = abs(count1-count2);
    if(count1-count2>0)
    {
        fast = headA;
        slow = headB;
    }
    else
    {
         fast = headB;
        slow = headA;
    }
    while(step--)
    {
        fast = fast->next;
    }
    while(fast && slow)
    {
       if(fast == slow)
       {
           tmp = fast;
           break;
       }
        fast = fast->next;
        slow = slow->next;
    }
    return tmp;
}

环形链表
链接: link.
给定一个链表,判断链表中是否有环。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。

在这里插入图片描述
示例 2:
输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。

在这里插入图片描述
示例 3:
输入:head = [1], pos = -1
输出:false
解释:链表中没有环。
在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    struct ListNode* fast = head;
   struct ListNode* slow = head;
    while(fast && fast->next && slow)
    {
        fast = fast->next->next;
        slow = slow->next;
        if(fast == slow)
            return true;
    }
        return false;
    
}
第一章 绪论作业答案(共50分) 一、分析如下程序中 (1)~ (10)各语句的频度。(每个1分,共10分) Ex( ) { int i , j , t ; (1) for( i=1 ; i<10 ; i++) //n = (2) printf(“\n %d” , i ); //n = (3) for(i=1; i<=2; i++) //n = (4) printf(“\n”); //n = (5) for(i=1; i<=9; i++) //n = { (6) for(j=1; j <= i ; j++) //n = { (7) t = i * j ; //n = (8) printf(“]”,t); //n = } (9) for(j=1; j 0) { if(x > 100) {x -= 10 ; y -- ;} else x ++ ; } 问if 语句执行了多少次?(2分) y--执行了多少次?(2分) x ++执行了多少次?(2分) 三、回答问题(共25分) 书中16页的起泡排序如下: void bubble_sort(int a[],int n){ //将a中整数序列重新排列成自小至大有序的整数序列。 for(i=n-1,change=TRUE;i>=1&&change;--i){ change=FALSE; for(j=0;ja[j+1]{a[j]<-->a[j+1];change=TRUE; } } }//bubble_sort 1.(共15分)分析该算法的最佳情况 ,最坏情况和平均情况下各自的时间复杂度(给出分析思路与过程)。 (1) 最佳情况的时间复杂度分析(5分): (2) 最坏情况的时间复杂度分析(5分): (3) 平均情况的时间复杂度分析(5分): 2.(共10分)比较与C语言书中的起泡排序异同,并从时空效率角度说明谁更优。 四、完成如下选择题(每3分,共9分)。 1. 设f为原操作,则如下算法的时间复杂度是( )。 for (i = 1; i*i=1;i--) for(j=1;jA[j+1]) A[j]与A[j+1]对换; 其中n为正整数,则算法在最坏情况下的时间复杂度为( )。 A.O(n) B.O(nlog2n) C. O(n3) D. O(n2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值