链表及经典问题
一、链表基础知识
查找结点O(n),插入节点O(1),删除节点O(1)
节点至少包括: 数据域 + 指针域
虚拟头节点的使用: 链表的头地址可能发生改变
1. 经典的链表实现方法
(一)
#include <iostream>
using namespace std;
struct Node {
Node (int data ):data(data), next(NULL) { }
int data;
Node* next;
};
int main()
{
Node* head = NULL;
head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
Node* p = head;
while (p != NULL) {
cout << p->data<<"->";
p = p->next;
}
return 0;
}
(二)
#include <iostream>
using namespace std;
int data[10]; // 数据域
int next[10]; // 指针域
// 在ind的节点后面 添加一个地址为p的节点,值为value
void add(int ind, int p, int val) {
next [ind] = p;
data [p] = val;
return ;
}
int main()
{
int head = 3;
data[3] = 0;
add(3, 5, 1);
add(5, 2, 2);
add(2, 7, 3);
int p = head;
while (p != 0) {
cout << data[p] << "->";
p = next[p];
}
return 0;
}
二、链表的典型应用场景
1. 操作系统内的动态内存分配
2. LRU缓存淘汰算法
在内存中CPU存取速度非常快,在硬盘中CPU读取速度较慢,因此将512GB的内容读取1GB在缓存中
缓存形象的维护方式(非底层真实方式)
三、经典面试题-链表的访问
1.环形链表
1)哈希表使用一片额外的存储区
这种做法需要一片额外的存储区,高效的存储:哈希表
2)快慢指针
如果链表有环,那么快慢指针一定会相遇,指向同一节点
求相遇节点
leetcode 141
/**
* 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 ) return false ;
//定义 p 为慢指针 ,q为快指针
ListNode * p =head , *q= head->next ;
while (p!= q && q && q->next ){
p = p -> next ;
q = q->next -> next ;
}
return q && q->next ;
}
};
求环的起始点
假设链表的起始点距离环的起始点,长度为a,则当满节点走到环的起始点时,快节点走了2a的长度。假设环的长度为a+x则快节点剩余的距离就是x,此时快节点想要追上慢节点,需要追x的距离。则当快节点追上慢节点时,慢节点在环里走了x步,则剩余的距离就是a。因此,相遇点距离环的起始点距离,与链表起始点距离环的起始点距离相同,都是a。
leetcode 142
/**
* 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 )return nullptr;
ListNode *p = head , *q = head ->next ;
while ( p!= q && q && q->next ){
p = p->next ;
q = q->next ->next ;
}
if ( q == nullptr || q->next == nullptr ) return nullptr ;
p = head ->next ;
q = head ->next ->next;
while ( p != q ){
p = p->next;
q = q->next->next ;
}
p = head;
while ( p!= q) {
p = p->next ;
q = q->next ;
}
return q ;
}
};
/**
* 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 )return nullptr;
ListNode *p = head , *q = head ;
if (q->next == nullptr )return nullptr ;
p = p->next ; q= q->next ->next ;
while ( p!= q && q && q->next ){
p = p->next ;
q = q->next->next ;
}
if (q == nullptr || q->next == nullptr ) return nullptr ;
p = head ;
while ( p != q ){
p = p->next ;
q = q->next ;
}
return q ;
}
};
leetcode202
class Solution {
public:
int getnext( int n ){
int z = 0 ;
while(n){
z += (n % 10 ) * (n % 10) ;
n /= 10 ;
}
return z ;
}
bool isHappy(int n) {
int p = n ,q = n ;
do{
p = getnext( p ) ;
q = getnext( getnext( q ) );
}while (p != q && q != 1 );
return q ==1 ;
}
};
四、经典面试题-链表的反转
1.链表的反转
首先,令cur指针所指向的节点指向pre指针所指向的节点
然后移动指针pre到cur所在的位置,移动cur到next所在的位置
注:cur指向已反转部分的头结点,pre指向未反转部分的头结点
leetcode 206
/**
* 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* reverseList(ListNode* head) {
if(head == nullptr )return nullptr ;
ListNode *pre = nullptr , *cur = head ,*p = cur->next ;
while(cur ){
cur->next = pre;
pre = cur ;
(cur = p ) && (p = p -> next) ; // 当cur指向nullptr时,p就不指向下一位了
}
return pre ;
}
};
递归回溯倒序输出
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == nullptr || head ->next == nullptr )return head ;
ListNode *tail = head ->next ,*p = reverseList(head->next ) ;
head->next = tail ->next ;
tail ->next = head ;
return p ;
}
};
反转链表的头n个节点
Node * reverse (Node *head , int n){
if (n == 1) return head ;
Node *tail = head->next; *p = revrese (head->next ,n-1) ;
head ->next = tail ->next ;
tail ->next = head
return p ;
}
2. 给出链表反转区间
leetcode 92
class Solution {
public:
ListNode *reverseN (ListNode *head ,int n){
if (n == 1) return head ;
ListNode*tail=head->next,*p=reverseN(head->next,n-1);
head->next = tail->next;
tail->next = head ;
return p ;
}
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode ret(0 , head) , *p = &ret ; // p 节点指向待反转区域的前一个结点// 虚拟头节点
int cnt = n-m+1;
while ( --m ) p = p->next;
p->next = reverseN(p->next , cnt );
return ret.next ;
}
};
3. k个一组反转链表
leetcode 25
class Solution {
public:
ListNode * __reverseN (ListNode *head , int n ){
if (n == 1 )return head ;
ListNode *tail = head -> next , *p = __reverseN(head ->next , n-1 ) ;
head->next = tail->next ;
tail->next = head ;
return p ;
}
ListNode *reverseN (ListNode *head , int n ){
ListNode *p =head ;
int cnt = n;
while( --n && p ) p= p->next ;
if ( p== nullptr )return head ;
return __reverseN(head , cnt );
}
ListNode* reverseKGroup(ListNode* head, int k) {
// 节点p为虚拟头节点,即待反转节点的前一个结点 节点q为待反转的头节点, 即反转之后的尾节点
ListNode ret (0 ,head ), *p = &ret ,*q =p->next ;
while ((p->next = reverseN(q ,k)) != q ){
p = q ;
q = p->next ;
}
return ret.next;
}
};
4. 旋转链表
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(head == nullptr )return nullptr ;
int n = 1 ;
ListNode *p = head ;
while (p->next ) p = p->next , n ++ ;
p->next = head ;
k %= n ;
k = n - k ;
while( k-- ) p = p->next ;
head = p->next ;
p->next = nullptr ;
return head ;
}
};
五、经典面试题-链表的删除
1. 删除链表的倒数第N个节点
leeetcode 19
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode ret (0,head) , *p = &ret , *q =head ;
while (n-- ) q = q->next ;
while ( q ) p = p->next , q= q ->next ;
p -> next = p->next ->next ;
return ret.next ;
}
};
2. 删除排序链表中的重复节点
leetcode 83
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == nullptr )return nullptr ;
ListNode *p = head ;
while ( p->next ){
if(p->val == p->next ->val ){
p->next = p->next ->next ;
}else {
p = p->next ;
}
}
return head ;
}
};
3.把链表中重复的节点全部删除
*leetcode 82 *
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode ret(0 , head) ,*p = &ret,*q = head ;
while (p->next){
if (p->next->next && p->next->val == p ->next->next->val){
q = p->next->next ;
while (q && q->val == p->next->val ){
q = q->next ;
}
p->next = q ;
}else{
p = p ->next ;
}
}
return ret.next ;
}
};