【数据结构】 ——单链表基础题【下】

1、单链表是否回文

在这里插入图片描述
思路1:取出单链表的结点放入栈中,再进行一一比较
思路2: 找出后半部分的元素入栈,在与前半部分的元素进行出栈比较

思路一:
class PalindromeList {
public:
    bool chkPalindrome(ListNode* A) 
    {
        ListNode* p=A;
        stack<int> list;
        while(p)
        {
            list.push(p->val);
            p=p->next;
        }
        p=A;
        while(p)
        {
            if(list.top()!=p->val)
            {
                return false;
            }
            else{
                list.pop();
                p=p->next;
            }
        }
        return true;
    }
};
思路二:
class PalindromeList {
public:
    ListNode* getMid(ListNode* head)
    {
        ListNode* fast=head;
        ListNode* slow=head;
        while(fast->next&&fast->next->next)
        {
            fast=fast->next->next;
            slow=slow->next;
        }
        return slow;
    }
    bool chkPalindrome(ListNode* A) 
    {
        ListNode* left=A;
        ListNode* right=getMid(A)->next;
        stack<int> s;
        while(right)
        {
            s.push(right->val);
            right=right->next;
        }
        while(s.empty())
        {
            if(left->val!=s.top())
            {
                return false;
            }
            else{
                s.pop();
                left=left->next;
            }
        }
        return true;
    }
};

2、相交链表

在这里插入图片描述在这里插入图片描述

class Solution
{
public:
	ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
	{
		ListNode* meet = NULL;
		ListNode* A = headA;
		ListNode* B = headB;
		int lenA = 0;
		int lenB = 0;
		while (A&&A->next)
		{
			lenA++;
			A = A->next;
		}
		while (B&&B->next)
		{
			lenB++;
			B = B->next;
		}
		if (A != B)
		{
			return NULL;
		}

		else
		{
			A = headA;
			B = headB;
			if (lenA > lenB)
			{
				int gap = lenA - lenB;
				while (gap--)
				{
					A = A->next;
				}
			}
			else
			{
				int gap = lenB - lenA;
				while (gap--)
				{
					B = B->next;
				}
			}
		}
		while (A&&B)
		{
			if (A == B)
			{
				meet = A;
				break;
			}
			A = A->next;
			B = B->next;
		}
		return meet;
	}
};

3、判断链表是否有环

在这里插入图片描述

class Solution {
public:
    bool hasCycle(ListNode *head) 
    {
        ListNode* slow=head;
        ListNode* fast=head;
        while(fast&&fast->next)
        {
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast)
            {
                return true;
            }
        }
        return false;
    }
};

4、环形链表||

在这里插入图片描述

class Solution {
public:
    ListNode *detectCycle(ListNode *head) 
    {
        ListNode* slow=head;
        ListNode* fast=head;
        ListNode* temp=NULL;
        while(fast&&fast->next)
        {
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast)
            {
                temp=slow;
                break;
            }
        }
        if(temp==NULL)
        {
            return NULL;
        }
        slow=head;
        while(slow!=temp)
        {
            slow=slow->next;
            temp=temp->next;
        }
        return temp;
    }
};

方法二:
//将找过的节点的值修改为一个绝对不会在链表中出现的值,只要找到具有这个特殊值的节点返回即可
class Solution {
public:
    ListNode *detectCycle(ListNode *head) 
    {
        ListNode* cur=head;
        while(cur)
        {
            if(cur->val==INT_MIN)
                    return cur;
            else
                cur->val=INT_MIN;
                cur=cur->next;
        }
        return NULL;
    }
};

5、复制带随机链表的指针

在这里插入图片描述

class Solution {
public:
    Node* copyRandomList(Node* head) 
    {
        map<Node*,int> mp;//地址到节点的map
        vector<Node*> nlist;//使用vector根据存储节点位置访问地址
        int i = 0;
        Node* ptr = head;
        while(ptr){//将新链表节点存入nlist,生成节点地址到位置的映射mp
            nlist.push_back(new Node(ptr->val));
            mp[ptr] = i;
            i++;
            ptr = ptr->next;
        }//记录节点位置
        i = 0;//再次遍历原始链表,连接新链表的next指针/random指针
        ptr = head;
        nlist.push_back(0);
        while(ptr){
            nlist[i]->next = nlist[i+1];//next指针
            if(ptr->random){//random指针不空时
                int id = mp[ptr->random];//根据node_map确认
                nlist[i]->random = nlist[id];//原链表random指针指向的位置是id
            }
            i++;
            ptr = ptr->next;
        }
        return nlist[0];
    }
};
发布了33 篇原创文章 · 获赞 13 · 访问量 1035

猜你喜欢

转载自blog.csdn.net/Vicky_Cr/article/details/105152250
今日推荐