LeetCode日记2020.2.2
文章目录
链表,今天题总体难度较前几天有所增加,以后要回顾总结。
1019 链表中的下一个更大节点(mid)
这道题虽然暴力解法能够通过,但是利用栈一次遍历才是该题真正的解法。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> nextLargerNodes(ListNode* head) {
stack<pair<int,int>> s;
vector<int> res;
int cnt = 0;
while(head!=NULL)
{
res.push_back(0);
int val = head->val;
while(!s.empty() && s.top().second<val)
{
res[s.top().first] = val;
s.pop();
}
s.push(pair<int, int>(cnt, val));
++cnt;
head=head->next;
}
return res;
}
};
142 环形链表2(mid)
比官方题解好懂
https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/shuang-zhi-zhen-qing-xi-ti-jie-zhen-zheng-cong-shu/
/**
* 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) {
ListNode *slow = head, *fast = head;
do{
if(fast==NULL||fast->next==NULL)
return NULL;
fast = fast->next->next;
slow = slow->next;
}while(slow!=fast);
slow=head;
while(slow!=fast)
{
slow=slow->next;
fast=fast->next;
}
return slow;
}
};
430 扁平化多级双向链表(mid)
一个变形的深度优先搜索,用栈实现。
注意子链表头中的prev为空并没有指向父节点,有点坑。
/*
// Definition for a Node.
class Node {
public:
int val;
Node* prev;
Node* next;
Node* child;
};
*/
class Solution {
public:
Node* flatten(Node* head) {
if(head==NULL)
return head;
stack<Node*> s;
s.push(head);
while(!s.empty())
{
auto p = s.top();
while(p->child==NULL&&p->next!=NULL)
p=p->next;
if(p->child!=NULL)
{
s.top()=p;
s.push(p->child);
}
else
{
s.pop();
if(!s.empty())
{
auto temp = s.top()->next;
s.top()->next = s.top()->child;
s.top()->child = NULL;
s.top()->next->prev=s.top();
p->next = temp;
s.top() = p;
if(temp!=NULL)
temp->prev = p;
}
}
}
return head;
}
};
138 复制带随即指针的链表(mid)
可以用复制图的算法,但官方题解的方法很巧妙。
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
if(head==NULL)
return head;
auto myhead = head;
while(head!=NULL)
{
Node* node = new Node(head->val);
auto temp = head->next;
head->next = node;
node->next = temp;
node->random = head->random;
head=head->next->next;
}
Node* dummyH = new Node(-1);
Node* newEnd = dummyH;
head = myhead;
while(head!=NULL)
{
if(head->next->random!=NULL)
head->next->random = head->next->random->next;
head = head->next->next;
}
head = myhead;
while(head!=NULL)
{
newEnd->next = head->next;
head->next = head->next->next;
head = head->next;
newEnd = newEnd->next;
}
newEnd->next = NULL;
return dummyH->next;
}
};
61 旋转链表(mid)
链表储存结构不同于数组,因此不要让处理数组的思维局限住。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(head==NULL || k==0)
return head;
auto End = head;
int cnt = 1;
while(End->next!=NULL)
{
End=End->next;
++cnt;
}
End->next = head;
k %= cnt;
k = cnt - k;
cnt = 1;
while(cnt<k)
{
head=head->next;
++cnt;
}
auto temp = head;
head=head->next;
temp->next = NULL;
return head;
}
};