1, ya sea de enlace simple lista palíndromo
Pensando 1: Eliminar el nodo de lista enlazada único colocado en la pila, una por una y luego comparar
las ideas 2: Identificar la segunda mitad de la pila de elemento, y la pila de elementos para la primera mitad de la comparación
思路一:
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, la lista de intersección
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, se determina si un plato de cadena
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, lista enlazada circular ||
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, la lista de punteros copia con Random
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];
}
};