当前位置: 首页> 财经> 创投人物 > 嵌入式软件开发面试问题_自由设计师是什么意思_四川省人民政府_企业宣传片视频

嵌入式软件开发面试问题_自由设计师是什么意思_四川省人民政府_企业宣传片视频

时间:2025/7/11 7:40:57来源:https://blog.csdn.net/weixin_53287225/article/details/142821549 浏览次数:0次
嵌入式软件开发面试问题_自由设计师是什么意思_四川省人民政府_企业宣传片视频

目录

LeetCode876 链表的中间节点

Leetcode141 环形链表

LeetCode142 环形链表II

LeetCode143 重排链表


LeetCode876 链表的中间节点

初始化时快慢指针同时指向head,快指针移动两步,慢指针移动两步。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* middleNode(ListNode* head) {ListNode* fast=head;ListNode* slow=head;while(fast&&fast->next){fast=fast->next->next;slow=slow->next;}return slow;}
};

Leetcode141 环形链表

 

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:bool hasCycle(ListNode *head) {ListNode* fast=head;ListNode* slow=head;while(fast&&fast->next){fast=fast->next->next;slow=slow->next;if(fast==slow){return true;}}return false;}
};

LeetCode142 环形链表II

 

 

/*** 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* fast=head;ListNode* slow=head;while(fast&&fast->next){fast=fast->next->next;slow=slow->next;if(fast==slow){while(head!=slow){head=head->next;slow=slow->next;}return slow;}}return nullptr;}
};

 

LeetCode143 重排链表

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* reverse(ListNode* head){ListNode* pre=nullptr;ListNode* cur=head;while(cur){ListNode* nex=cur->next;cur->next=pre;pre=cur;cur=nex;}return pre;}void reorderList(ListNode* head) {ListNode* fast=head;ListNode* slow=head;while(fast&&fast->next){fast=fast->next->next;slow=slow->next;}ListNode* last=reverse(slow);ListNode* r1=head;ListNode* r2=last;while(r2->next){ListNode* nex1=r1->next;ListNode* nex2=r2->next;r2->next=r1->next;r1->next=r2;r1=nex1;r2=nex2;}}
};

 

关键字:嵌入式软件开发面试问题_自由设计师是什么意思_四川省人民政府_企业宣传片视频

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: