乡镇可以做门户网站,平顶山城市建设局网站,wordpress图挂了,网站图片特效源码题目链接
力扣#xff08;LeetCode#xff09;官网 - 全球极客挚爱的技术成长平台 题目解析 分析题目后我们可以直接进行模拟实现。 具体用到的就是我们之前的知识的结合#xff0c;首先使用快慢指针找到链表的中间结点。然后将后半段链表给翻转一下#xff0c;然后再让这…题目链接
力扣LeetCode官网 - 全球极客挚爱的技术成长平台 题目解析 分析题目后我们可以直接进行模拟实现。 具体用到的就是我们之前的知识的结合首先使用快慢指针找到链表的中间结点。然后将后半段链表给翻转一下然后再让这两个链表进行合并。 代码
/*** 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* middleListNode(ListNode* head){ListNode* slowhead,* fasthead;while(fast-next!nullptrfast-next-next!nullptr){slowslow-next;fastfast-next-next;}return slow;}// 翻转链表ListNode* reverselist(ListNode* l){ListNode* curl;ListNode* prevnullptr;while(cur){ListNode* nextcur-next;cur-nextprev;prevcur;curnext;}return prev;}// 合并链表void reorderList(ListNode* head) {if(headnullptr||head-nextnullptr||head-next-nextnullptr) return;ListNode* midnodemiddleListNode(head);ListNode* l1head;ListNode* l2midnode-next;midnode-nextnullptr;l2reverselist(l2);ListNode* pheadnew ListNode(0);ListNode* tempphead;while(l1||l2){if(l1){temp-nextl1;l1l1-next;temptemp-next;}if(l2){temp-nextl2;l2l2-next;temptemp-next;}}// ListNode* l1next;// ListNode* l2next;// while(l1!nullptrl2!nullptr)// {// l1nextl1-next;// l2nextl2-next;// l1-nextl2;// l1l1next;// l2-nextl1;// l2l2next;// }}
};