环形链表:
哈希表 count, insert
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
//通过哈希表遍历所有节点,存储节点
unordered_set<ListNode*> seen;
while(head!=nullptr){
if(seen.count(head)){
return true;
}
seen.insert(head);
head=head->next;
}
return false;
}
};
/*
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){}
};*/
双指针:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
//需要先判断是否存在链表
if (head == nullptr || head->next == nullptr) {
return false;
}
//快慢指针
ListNode* fast =head;
ListNode* slow = head;
//更新指针
while(fast!=NULL&&fast->next!=NULL)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
return true;
}
}
return false;
}
};
/*
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){}
};*/
环形指针2:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
//判断存在否
if(head==nullptr||head->next==nullptr){
return nullptr;
}
//快慢指针;
ListNode* fast=head;
ListNode* slow=head;
while(fast!=NULL&&fast->next!=NULL){
//更替指针
slow = slow->next;
fast = fast->next->next;
if(slow==fast){
//记录交点 index2 : x=(n-1)(y+z)+z
ListNode* index1 = head;
ListNode* index2 = fast;
while(index1!=index2){
//链表指针下移
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return nullptr;
}
};
19.删除链表的倒数第N个结点
双指针:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
//如果存在n的位置不存在,最好先获取链表的长度
//快慢指针,使用虚拟头节点,方便删除头节点
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* slow = dummyHead;
ListNode* fast = dummyHead->next;
//快指针先走n+1步
while(n--){
fast = fast->next;
}
//接下来一起走,直到fast到null
while(fast!=nullptr){
fast = fast->next;
slow = slow->next;
}
//删除slow的下一个节点
ListNode* tmp= slow->next;
slow->next =slow->next->next;
delete(tmp);
tmp = nullptr;
return dummyHead->next;
}
};
单调栈:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
//根据栈「先进后出」的原则,我们弹出栈的第 nnn 个节点就是需要删除的节点
ListNode* dummy= new ListNode(0,head);
stack<ListNode*> stk;
ListNode* cur=dummy;
while(cur){
stk.push(cur);
cur = cur->next;
}
//删除了倒数n个节点
for(int i=0;i<n;++i){
stk.pop();
}
//获得删除节点的前驱
ListNode* prev = stk.top();
ListNode* tmp1 = prev->next;
prev->next = prev->next->next;
delete tmp1;
tmp1 = nullptr;
return dummy->next;
}
};
160.相交链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
//指针指向同一块 curA=curB
//计算链表的长度
int lenA=0,lenB=0;
ListNode* curA= headA;
ListNode* curB= headB;
while(curA){
++lenA;
curA=curA->next;
}
while(curB){
++lenB;
curB=curB->next;
}
//重新回到起点
curA=headA;
curB=headB;
//curA只想最大值
if(lenA<lenB){
swap(lenA,lenB);
swap(curA,curB);
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap--) {
curA = curA->next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != NULL) {
if (curA == curB) {
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};