当前位置:网站首页>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;
}
};
边栏推荐
- Interface idempotency
- Wechat applet related operation examples
- STL notes (III): input / output stream
- 自己实现is_convertible
- 搭建私有CA服务器
- Solution of win11 blue screen code 0x0000001a
- typora+PicGo+阿里云OSS 搭建以及报错解决【转载】
- Oracle split branches
- Guanghetong and Intel released the global version of 5g communication module
- Matter's Unified Smart Home connection standard enables local automatic interaction between smart devices
猜你喜欢
随机推荐
05. Libavformat Library of ffmpeg
Typera+picgo+ Alibaba cloud OSS setup and error reporting solution [reprint]
基环树入门
STL notes (III): input / output stream
Interface idempotency
Delivery practice of private PAAS platform based on cloud native
Implement is by yourself_ convertible
Use getifaddrs to obtain the IP address of the local network interface
1310_一个printf的实现分析
[untitled]
JWT(json web token)
I have seven schemes to realize web real-time message push, seven!
STL notes (VII): container deque
38 lines of PHP code free import database analysis Linux access log
06. Libavdevice Library of ffmpeg
如何判断是否遭到DDOS攻击
[cloud co creation] design Huawei cloud storage architecture with the youngest cloud service hcie (Part 1)
LCP插件创建对等VLAN接口
Win11 how to view the file explorer tab
Implement is by yourself_ class








