回文链表~链表分割~相交链表

这篇博客介绍了如何高效地处理链表问题,包括检查链表是否为回文结构,如何分割链表使得小于特定值的节点在前,以及寻找两个链表的相交节点。这些问题都通过O(n)时间复杂度和O(1)额外空间复杂度的算法解决,展示了对链表操作的深入理解和技巧。
摘要由CSDN通过智能技术生成

回文链表

描述

对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。

给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。

测试样例:

1->2->2->1
返回:true
class PalindromeList {
public:
	bool chkPalindrome(ListNode* A) {
		if (A == NULL || A->next == NULL)
			return true;
		ListNode* slow, *fast, *prev, *cur, *nxt;
		slow = fast = A;
		//找到中间节点
		while (fast && fast->next)
		{
			slow = slow->next;
			fast = fast->next->next;
		}
		prev = NULL;
		//后半部分逆置
		cur = slow;
		while (cur)
		{
			nxt = cur->next;
			cur->next = prev;
			prev = cur;
			cur = nxt;
		}
		//逐点比对
		while (A && prev)
		{
			if (A->val != prev->val)
				return false;
			A = A->next;
			prev = prev->next;
		}
		return true;
	}
};

 链表分割

描述

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

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        ListNode *lowhead,*lowtail;
        ListNode *highhead,*hightail;
        ListNode* cur=pHead;
        //一大一小
        lowtail=lowhead=(ListNode*)malloc(sizeof (* pHead) );
        hightail=highhead=(ListNode*)malloc(sizeof(*pHead));
        while(cur)
        {
            if(cur->val<x)
            {
                lowtail->next=cur;
                lowtail=cur;
            }
            else
            {
                hightail->next=cur;
                hightail=cur;
            }
            cur=cur->next;
        }
        //把这两个链表接起来
        lowtail->next=highhead->next;
        //这里有个易错点,注意把大的尾巴置空  否者可能出现环形链表
        hightail->next=NULL;
        ListNode*tmp=lowhead->next;
        //释放两个头
        free(highhead);
        free(lowhead);
        return tmp;
    }
};

相交链表 

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

图示两个链表在节点 c1 开始相交:

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode*a=headA;
    int n1=0,n2=0;
    struct ListNode*b=headB;
    while(a)
    {
        a=a->next;
        n1++;
    }
    while(b)
    {
        b=b->next;
        n2++;
    }
    if(a==b)
    {
     
     if(n1>n2)
     {
         struct ListNode*fast=headA;
     struct ListNode*slow=headB;
         int n=n1-n2;
         while(n--)
         {
             fast=fast->next;
         }
         while(n2--)
         {
             if(fast==slow)  return fast;
             fast=fast->next;
             slow=slow->next;
         }
     }
     else{
         int n=n2-n1;
         struct ListNode*fast=headB;
     struct ListNode*slow=headA;
      while(n--)
         {
             fast=fast->next;
         }
         while(n2--)
         {
             if(fast==slow)  return fast;
             fast=fast->next;
             slow=slow->next;
         }
     }
    }
   return NULL;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谁家的攻城狮

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

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

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

打赏作者

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

抵扣说明:

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

余额充值