当前位置:网站首页>Sword finger offer special shock edition day 9
Sword finger offer special shock edition day 9
2022-07-25 05:20:00 【hys__ handsome】
The finger of the sword Offer II 027. Palindrome list
O ( n ) O(n) O(n) Time complexity and O ( 1 ) O(1) O(1) Spatial complexity
class Solution {
public:
ListNode* mid_node(ListNode* head) {
ListNode *slow = head, *fast = head;
while(fast->next != nullptr && fast->next->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
ListNode* reverse_list(ListNode *head){
ListNode *pre = nullptr, *cur = head, *tmp;
while(cur != nullptr) {
tmp = cur;
cur = cur->next;
tmp->next = pre;
pre = tmp;
}
return pre;
}
bool isPalindrome(ListNode* head) {
if(head == nullptr) return true;
auto i = head, j = mid_node(head)->next;
j = reverse_list(j);
while(i != nullptr && j != nullptr) {
if(i->val != j->val) return false;
i = i->next, j = j->next;
}
return true;
}
};
The finger of the sword Offer II 028. Flatten multi-level bidirectional linked list
- First recursively flatten the linked list ( Just make sure next The pointer )
- Then iterate again to empty child The pointer , Reassign prev The pointer
class Solution {
public:
Node* last_ptr(Node *head) {
Node *cur = head, *pre = head->prev;
while(cur != nullptr) {
if(cur->child != nullptr) {
auto tmp = cur->next;
cur->next = cur->child;
auto child_last = last_ptr(cur->child);
child_last->next = tmp;
pre = child_last;
cur = tmp;
} else {
pre = cur;
cur = cur->next;
}
}
return pre;
}
Node* flatten(Node* head) {
if(head == nullptr) return nullptr;
last_ptr(head);
auto cur = head , prev = head->prev;
while(cur != nullptr) {
cur->child = nullptr;
cur->prev = prev;
prev = cur;
cur = cur -> next;
}
return head;
}
};
The finger of the sword Offer II 029. Sorted circular linked list
simulation , Distinguish between various situations
class Solution {
public:
Node* insert(Node* head, int insertVal) {
if(head == nullptr) {
head = new Node(insertVal);
head->next = head;
} else {
if(head == head->next) {
// Single point loop
head->next = new Node(insertVal,head);
} else {
Node *cur = head->next, *pre = head;
while(cur != head) {
// Between maximum and minimum ,insertValue It can be inserted if it is the maximum or minimum
if(cur->val < pre->val && (insertVal >= pre->val || insertVal <= cur->val)) {
pre->next = new Node(insertVal, cur);
break;
}
// Values in cur and pre Can be inserted between
if(insertVal >= pre->val && insertVal <= cur->val) {
pre->next = new Node(insertVal, cur);
break;
}
pre = cur;
cur = cur->next;
}
// When the elements in the list are equal
if(cur == head) {
pre->next = new Node(insertVal, cur);
}
}
}
return head;
}
};
边栏推荐
- 剑指offer专项突击版第9天
- Exchange 2010 SSL certificate installation document
- Add click event to unity 3D object
- What about reinstalling win11 system?
- STL notes (III): input / output stream
- 300. Longest increasing subsequence
- STL notes (V): iterator
- How do novices open accounts for stock speculation? Is it safe for securities companies to open accounts online?
- LCP插件创建对等VLAN接口
- nacos中哪边有这个列的sql脚本啊?
猜你喜欢

What about reinstalling win11 system?

When image component in wechat applet is used as background picture

STL notes (III): input / output stream

Teach you how to locate unreasonable SQL? And optimize it

85 distributed project construction

Implement is by yourself_ base_ of

基于云原生的私有化 PaaS 平台交付实践

ping命令
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
随机推荐
ZTE's first 5g mobile phone, axon 10 pro, was launched in the first half of this year
Uniapp custom application exit execution content
epoll的实现原理
Why does the official not recommend using UUID as MySQL primary key
一篇文章带你读懂Redis的哨兵模式
Typera+picgo+ Alibaba cloud OSS setup and error reporting solution [reprint]
接口幂等性
deep报错
Anshi semiconductor management paid a visit to Wentai technology and Gree Electric appliance
[small program practice] first day
RHCE first day
Build keyword driven automated testing framework
Shenzhen on call test, subject 4 on call test, subject 3 theory, second theory on call test instructions
Pikachu vulnerability platform exercise
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
Seven suggestions for Server Protection
HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier
LCP插件创建对等VLAN接口
项目管理工具——阿里云Projex介绍与实战
[untitled]