[] Data structure - the basis of a single list under the title []

1, whether singly linked list palindrome

Here Insert Picture Description
Thinking 1: Remove the single linked list node placed on the stack, one by one and then compare
ideas 2: identify the second half of the element stack, and the stack of elements for the first half of the comparison

思路一:
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, intersection list

Here Insert Picture DescriptionHere Insert Picture Description

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, it is determined whether a chain ring

Here Insert Picture Description

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, circular linked list ||

Here Insert Picture Description

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, the copy pointer list with Random

Here Insert Picture Description

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];
    }
};
Published 33 original articles · won praise 13 · views 1035

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/105152250