当前位置:网站首页>剑指offer专项突击版第9天
剑指offer专项突击版第9天
2022-07-25 05:16:00 【hys__handsome】
O ( n ) O(n) O(n)时间复杂度和 O ( 1 ) O(1) O(1) 空间复杂度
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;
}
};
- 先递归的偏平化链表(只确定next指针)
- 然后再迭代一次清空child指针,重新赋值prev指针
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;
}
};
剑指 Offer II 029. 排序的循环链表
模拟,分清楚各种情况
class Solution {
public:
Node* insert(Node* head, int insertVal) {
if(head == nullptr) {
head = new Node(insertVal);
head->next = head;
} else {
if(head == head->next) {
//单点循环
head->next = new Node(insertVal,head);
} else {
Node *cur = head->next, *pre = head;
while(cur != head) {
//在最大和最小之间时,insertValue是最大或最小即可插入
if(cur->val < pre->val && (insertVal >= pre->val || insertVal <= cur->val)) {
pre->next = new Node(insertVal, cur);
break;
}
//值在cur和pre之间时可插入
if(insertVal >= pre->val && insertVal <= cur->val) {
pre->next = new Node(insertVal, cur);
break;
}
pre = cur;
cur = cur->next;
}
//列表中元素都相等时
if(cur == head) {
pre->next = new Node(insertVal, cur);
}
}
}
return head;
}
};
边栏推荐
- Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?
- Forwarding and sharing function of wechat applet
- 38 lines of PHP code free import database analysis Linux access log
- 基于云原生的私有化 PaaS 平台交付实践
- Preliminary understanding of Panda3D particle system
- 微信小程序相关操作示例
- RHCE first day
- [untitled]
- Logu p3398 hamsters find sugar solution
- Compile ue5.0
猜你喜欢

Wechat official account all article download links to get

Solution of win11 blue screen code 0x0000001a

Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?

Redis cluster setup (Windows)

epoll的实现原理

Implementation principle of epoll

Forwarding and sharing function of wechat applet
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

Thesis reading | which is the best multilingual pre training technology for machine translation? See the latest progress!
![[no title] 1](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[no title] 1
随机推荐
Baklib: share some methods about building enterprise knowledge management (km)
基于云原生的私有化 PaaS 平台交付实践
Redis cluster setup (Windows)
Docker builds MySQL master-slave replication
[untitled]
300. Longest increasing subsequence
In depth understanding of pre post + +, -- and negative modulus
Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies
Teach you how to locate unreasonable SQL? And optimize it
[globally unique ID] how to handle the ID primary key after dividing the database and table?
Purpose of setting novice task period in the integral system
China trifluoroethanol industry research and investment forecast report (2022 Edition)
"Niuke | daily question" inverse Polish expression
AUTOSAR from getting started to mastering 100 lectures (105) - protection mechanism of AUTOSAR timing for functional safety
Androd releases jitpack open source project (gradle7.2)
An article takes you to understand the sentinel mode of redis
Wechat official account all article download links to get
初步了解Panda3d粒子系统
Redis的三个必知必会的问题
Compile ue5.0