当前位置:网站首页>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;
}
};
边栏推荐
- What about reinstalling win11 system?
- 初步了解Panda3d粒子系统
- Learning records [email protected] R & D effectiveness measurement indicators
- 接口幂等性
- [small program practice] first day
- 如何判断是否遭到DDOS攻击
- ZTE's first 5g mobile phone, axon 10 pro, was launched in the first half of this year
- STL notes (IV): String
- Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?
- 38 lines of PHP code free import database analysis Linux access log
猜你喜欢

Implement is by yourself_ class

Necessary skills for mobile terminal test: ADB command and packet capturing

When image component in wechat applet is used as background picture

Project management tools - project developer tools

Panda3D keyboard moving scene

STL notes (I): knowledge system

JWT(json web token)

Vim查找替换及正则表达式的使用

An article takes you to understand the sentinel mode of redis
[small program practice] first day
随机推荐
Pikachu vulnerability platform exercise
I will write some Q & A blogs recently, mainly focusing on the points that are easy to have doubts.
The third day of rhcsa summer vacation
Now the operator wants to check the answer details of all user questions from Zhejiang University. Please take out the corresponding data
搭建私有CA服务器
Anshi semiconductor management paid a visit to Wentai technology and Gree Electric appliance
unity 3D物体添加 点击事件
STL notes (I): knowledge system
Guanghetong and Intel released the global version of 5g communication module
Project management tools - project developer tools
Special analysis of data security construction in banking industry
How to carry out the function test with simple appearance and complex interior?
Implement is by yourself_ base_ of
Panda3D keyboard moving scene
LCP插件创建对等802.1ad接口
When image component in wechat applet is used as background picture
RHCE first day
Redis的三个必知必会的问题
Leetcode 15: sum of three numbers
Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?