• 【牛客网】链表中倒数第k个结点、CM11 链表分割、OR36 链表的回文结构


    🧑‍💻作者: @情话0.0
    📝专栏:《牛客网》
    🔖题目链接:链表中倒数第k个结点、CM11 链表分割、OR36 链表的回文结构


    一、链表中倒数第k个结点

    输入一个链表,输出该链表中倒数第k个结点。

    示例

    输入:1,{1,2,3,4,5}
    返回值:{5}

    理解思路:

      对于这道题,同样通过快慢指针的思想去解决,但是这个快指针不是每次向后移动两步,而是先让快指针走上k步,然后快慢指针再同时移动,当快指针指向空的时候慢指针指向的结点刚好为所要找的结点。除此之外,还要考虑到一些特殊情况,比如只有三个结点,却要返回倒数第四个结点;或者返回倒数第零个结点。

    代码

    struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
        struct ListNode* fast = pListHead;
        struct ListNode* slow = pListHead;
        while(k--)
        {
            if(fast==NULL) //有可能快指针提前指向空
            {
                return NULL;
            }
            fast=fast->next;
        }
        while(fast)
        {
            fast=fast->next;
            slow=slow->next;
        }
        return slow;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    二、链表分割

      现有一链表的头指针 ListNode* pHead,给一定值 x ,编写一段代码将所有小于 x 的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。

    示例

    输入:3,{2,4,1,5,3}
    返回值:{2,1,4,5,3}

    理解思路:

      对于这道题,有两种解决办法,但是大致思路大差不差。一种是带头结点的解法,一种是不带头结点的解法。对于不带头结点的方法:定义两个指针lesstailgreattail然后从头遍历链表,将相应的结点尾插到对应的头结点后,同时两个指针指向刚插入的结点,最后再将两个链表链接在一起;对于不带头结点的方法:定义四个指针greatHeadgreattaillessHeadlesstail,分割节点的方法与另外一种一样,但只需移动尾指针即可。但是这种办法要考虑到一种情况:链表中的结点全都大于x或着都小于x,对于这种情况,只需返回那个头指针非空的链表。

    代码1(带头结点)

    class Partition {
    public:
        ListNode* partition(ListNode* pHead, int x) {
            // write code here
            ListNode head1(0);
            ListNode head2(0);
            ListNode* greattail = &head1;
            ListNode* lesstail = &head2;
            ListNode* cur = pHead;
            while(cur)
            {
                pHead = cur->next;
                if(cur->val < x)
                {
                    lesstail->next = cur;
                    lesstail = cur;
                }
                else{
                    greattail->next = cur;
                    greattail = cur;
                }
                cur = pHead;
            }
            greattail->next = NULL;
            lesstail->next = head1.next;
            return head2.next;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    代码2(不带头结点)

    class Partition {
    public:
        ListNode* partition(ListNode* pHead, int x) 
        {
            ListNode* greatHead=NULL,*greattail=NULL;
            ListNode* lessHead=NULL,*lesstail=NULL;
            ListNode* cur=pHead;
            while(cur)
            {
                if(cur->val<x)
                {
                    if(lessHead==NULL)
                    {
                        lessHead=cur;
                    }
                    else
                    {
                        lesstail->next=cur;
                    }
                    lesstail=cur;
                }
                else
                {
                    if(greatHead==NULL)
                    {
                        greatHead=cur;
                    }
                    else
                    {
                        greattail->next=cur;
                    }
                    greattail=cur;
                }
                cur=cur->next;
            }
            if(lessHead==NULL) //特殊情况:链表结点全是大于x或者小于x的
            {
                return greatHead;
            }
            if(greatHead==NULL)
            {
                return lessHead;
            }
            lesstail->next=greatHead;
            greattail->next=NULL;
            return lessHead;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    三、链表的回文结构

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

    示例

    1->2->2->1
    返回:true

    理解思路1:

      题目中说链表的长度小于等于900,那么就可以对链表进行遍历,将所有的结点数据存放到数组当中,然后再定义一个左指针和一个右指针进行比对。
      当然这种解法只是刚好能解决这道题,但是若链表长度未给出区间,那么这种方法就不可取。

    理解思路2:

      这种解题思路就不需要考虑链表的长度。首先找出链表的中间结点,再从中间节点开始将中间结点以及后续结点反转得到一个新链表,然后对这两个链表进行比对,直到新链表为空时就判断停止,返回true,若在循环判断中发现了结点数值不一样,则返回false

    代码1

    class PalindromeList {
    public:
        bool chkPalindrome(ListNode* A) {
            int arr[900]={0};
            int i=0;
            int size=0;
            while(A)
            {
                arr[i++]=A->val;
                A=A->next;
                size++;
            }
            i=0;
            while(i<size)
            {
                if(arr[i]!=arr[size-1])
                {
                    return false;
                }
                i++;
                size--;
            }
            return true;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    代码2

    class PalindromeList {
    public:
    	//寻找中间节点
        struct ListNode* middleNode(struct ListNode* head)
        {
            struct ListNode* fast=head;
            struct ListNode* slow=head;
            while(fast&&fast->next)
            {
                fast=fast->next->next;
                slow=slow->next;
            }
            return slow;
        }
    	//从中间结点开始将后续链表反转
        struct ListNode* reverseList(struct ListNode* head)
        {
            struct ListNode* cur=head;
            struct ListNode* p=NULL;
            struct ListNode* q=NULL;
            while(cur)
            {
                q=cur->next;
                cur->next=p;
                p=cur;
                cur=q;
            }
            return p;
        }
        bool chkPalindrome(ListNode* A) {
            struct ListNode* midnode = middleNode(A);
            struct ListNode* rhead = reverseList(midnode);
            while(rhead)
            {
                if(A->val!=rhead->val)
                {
                    return false;
                }
                A=A->next;
                rhead=rhead->next;
            }
            return true;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    【STL容器】list
    const常量和基础数据类型
    csdn,是时候说再见!
    连接查询-mysql详解(五)
    MobTech ShareSDK 高级接口及配置
    pve安装ubuntu 调整 逻辑卷全在根目录
    B码的相关知识点笔记
    性能分析5部曲:瓶颈分析与问题定位,如何快速解决瓶颈?
    React源码分析1-jsx转换及React.createElement
    kettle学习总结(1)
  • 原文地址:https://blog.csdn.net/weixin_47648037/article/details/127796200