当前位置: 首页 > wzjs >正文

新闻资讯网站备案新泰网站设计

新闻资讯网站备案,新泰网站设计,简约大气网站模板,西安有哪些网站建设公司题目链接 反转链表:206. 反转链表 - 力扣(LeetCode)K个一组翻转链表:25. K 个一组翻转链表 - 力扣(LeetCode)回文链表:234. 回文链表 - 力扣(LeetCode) 实现思路 代码实…

题目链接

  • 反转链表:206. 反转链表 - 力扣(LeetCode)
  • K个一组翻转链表:25. K 个一组翻转链表 - 力扣(LeetCode)
  • 回文链表:234. 回文链表 - 力扣(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* reverseList(ListNode* head) {ListNode* pre = nullptr;ListNode* cur = head;while(cur) {ListNode* next = cur -> next;cur -> next = pre;pre = cur;cur = next;}return pre;}
};
  • K个一组翻转链表
/*** 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:vector<ListNode*> reverse(ListNode* head, ListNode* next) {ListNode* pre = next;ListNode* cur = head;while (cur != next) {ListNode* nextNode = cur -> next;cur -> next = pre;pre = cur;cur = nextNode;}return {pre, head}; // pre是反转之后的头节点,head是反转之后的尾节点}ListNode* reverseKGroup(ListNode* head, int k) {int cnt = 0;ListNode* cur = head;ListNode* preHead = new ListNode(-1);preHead -> next = head;ListNode* pre = preHead;while (cur) {cnt++;if (cnt % k == 0) {cnt = 0;vector<ListNode*> res = reverse(pre->next, cur->next);pre->next = res[0];cur = res[1];pre = cur;}cur = cur -> next;}return preHead -> next;}
};
  • 回文链表
/*** 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* next = cur -> next;cur -> next = pre;pre = cur;cur = next;}return pre;}bool isPalindrome(ListNode* head) {if (head -> next == nullptr) return true;ListNode* preHead = new ListNode();preHead -> next = head;ListNode* slow = preHead;ListNode* fast = preHead;while (fast != nullptr && fast -> next != nullptr) {slow = slow -> next;fast = fast -> next -> next;}// 反转slow->next到tail的部分ListNode* head1 = slow -> next;head1 = reverse(head1);ListNode* cur = head;ListNode* cur1 = head1;while (cur1) {if (cur -> val != cur1 -> val) {return false;}cur = cur -> next;cur1 = cur1 -> next;}return true;}
};

http://www.dtcms.com/wzjs/433162.html

相关文章:

  • 阐述电子商务网站的建设流程网站排名软件有哪些
  • 广州做网站新锐博客
  • 如何美化wordpress主题广告优化师的工作内容
  • 北京哪里制作网站成都门户网站建设
  • 广州网站搭建快速提升网站排名seo营销的概念
  • 汝州建设局网站培训体系
  • 毕业设计网站选题东莞做好网络推广
  • 临沂在线上网站建设seo收索引擎优化
  • 怎么样网站速度快常德网站设计
  • 黄埔区建设局网站想建立自己的网站怎么建立
  • 长沙做网站建设的网站投放广告费用
  • 武威做网站的大数据营销精准营销
  • 营销网站案例seo排名优化推广报价
  • 网站如何做搜索功能的百度教育网站
  • 网站设计官网好的搜索引擎推荐
  • wordpress怎么建设网站品牌关键词优化
  • 做外文翻译的网站高德北斗导航
  • 柞水县城乡建设局网站广州seo关键词优化外包
  • 淘宝客网站怎么批量采集淘宝商品方维采集淘宝数据思路百度热搜关键词排名优化
  • 手机销售网站的建设网络推广图片
  • 今日体育新闻最新消息朝阳区seo搜索引擎优化怎么样
  • 如何利用源码做网站百度推广登录平台网址
  • 漳州 做网站临沂seo网站管理
  • 网站怎样做超链接网站推广的常用方法有哪些?
  • 做灯饰的企业都会在哪些网站电商网站建设 网站定制开发
  • 搭建平台载体四川seo技术培训
  • 北京 网站策划公司怎么创建网站平台
  • asp.net网站建设实战 pdf技术短期培训班
  • 门户网站编辑联系方式合肥网站优化方案
  • 做网站需要什么手续资料视频营销