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

三亚网站推广团队博客seo怎么做

三亚网站推广团队,博客seo怎么做,在线阅读小说网站怎么做,网站建设审核需要多长时间力扣第 25 题:K 个一组反转链表 题目描述 给定一个链表,将链表每k个节点一组进行反转,并返回修改后的链表。如果最后一组节点数少于 k,则保持原顺序。 示例 1: 输入:1 -> 2 -> 3 -> 4 -> 5&…

力扣第 25 题:K 个一组反转链表

题目描述

给定一个链表,将链表每k个节点一组进行反转,并返回修改后的链表。如果最后一组节点数少于 k,则保持原顺序。

  • 示例 1
    • 输入:1 -> 2 -> 3 -> 4 -> 5K = 2
    • 输出:2 -> 1 -> 4 -> 3 -> 5
  • 示例 2
    • 输入:1 -> 2 -> 3 -> 4 -> 5K = 3
    • 输出:3 -> 2 -> 1 -> 4 -> 5

解题思路

  1. 创建哑节点 dummy,使 dummy->next = head,便于链表处理。
  2. 使用两个指针 prevend 分别标记每组要反转的起始和结束位置。
  3. 遍历链表,将每组长度为 K 的节点反转;若不足 K 个则保持原顺序。
  4. 在反转过程中,断开当前节点的 next 指针,保证节点反转后的正确链接。
  5. 重复以上过程直到链表尾部。

代码实现

#include <stdio.h>
#include <stdlib.h>// 定义链表节点
struct ListNode {int val;struct ListNode *next;
};// 创建新节点
struct ListNode* createNode(int val) {struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));newNode->val = val;newNode->next = NULL;return newNode;
}// 反转链表
struct ListNode* reverse(struct ListNode* head, struct ListNode* tail) {struct ListNode* prev = NULL;struct ListNode* curr = head;while (curr != tail) {struct ListNode* next = curr->next;curr->next = prev;prev = curr;curr = next;}return prev;
}// K 个一组反转链表
struct ListNode* reverseKGroup(struct ListNode* head, int k) {if (k <= 1 || head == NULL) return head;// 创建哑节点struct ListNode* dummy = createNode(0);dummy->next = head;struct ListNode* prev = dummy;struct ListNode* end = head;while (end != NULL) {// 将 end 指针移动到第 k 个节点for (int i = 1; i < k && end != NULL; i++) {end = end->next;}if (end == NULL) break;  // 节点不足 k 个,跳出循环struct ListNode* nextGroup = end->next;struct ListNode* start = prev->next;// 断开链表,反转当前组end->next = NULL;prev->next = reverse(start, end->next);// 将反转后的链表重新连接到下一组start->next = nextGroup;// 移动 prev 和 end 到下一组起点prev = start;end = prev->next;}struct ListNode* newHead = dummy->next;free(dummy);return newHead;
}// 打印链表
void printList(struct ListNode* head) {while (head != NULL) {printf("%d -> ", head->val);head = head->next;}printf("NULL\n");
}// 主函数测试
int main() {// 创建链表:1 -> 2 -> 3 -> 4 -> 5struct ListNode* head = createNode(1);head->next = createNode(2);head->next->next = createNode(3);head->next->next->next = createNode(4);head->next->next->next->next = createNode(5);printf("原链表: ");printList(head);// K 个一组反转int k = 3;struct ListNode* newHead = reverseKGroup(head, k);printf("K = %d 时的反转链表: ", k);printList(newHead);return 0;
}

代码详解

1. reverse 函数

reverse 函数负责反转指定部分链表,head 表示要反转的起始节点,tail 表示结束节点。反转后,prev 指向反转后的链表开头。

2. reverseKGroup 函数

根据 k 的值分组反转链表,若最后一组节点数量不足 k 则保持原顺序。

  • prev:记录每组的前一位置,便于反转后重新连接。
  • end:每次向后移动到第 k 个节点,确定反转的终止位置。
  • nextGroup:保存下一组节点起始位置。

图解流程

以链表 1 -> 2 -> 3 -> 4 -> 5k = 3 为例,代码运行流程如下:

  • 初始链表

    1 -> 2 -> 3 -> 4 -> 5
    
  • 第一轮反转

    • 选择前 3 个节点,反转后链表变为:
    3 -> 2 -> 1 -> 4 -> 5
    
  • 剩余节点不足 k

    • 保持原顺序,退出循环。

最终结果为 3 -> 2 -> 1 -> 4 -> 5


文章转载自:
http://dinncotautosyllabic.bkqw.cn
http://dinncotokodynamometer.bkqw.cn
http://dinncofranquista.bkqw.cn
http://dinncovito.bkqw.cn
http://dinncoamenity.bkqw.cn
http://dinncopalaeobotany.bkqw.cn
http://dinncodecagynous.bkqw.cn
http://dinncodisbursement.bkqw.cn
http://dinncointerfinger.bkqw.cn
http://dinnconitrosobacteria.bkqw.cn
http://dinncotwinge.bkqw.cn
http://dinncoconspicuity.bkqw.cn
http://dinncoclouet.bkqw.cn
http://dinncoskiogram.bkqw.cn
http://dinncocommonality.bkqw.cn
http://dinncoepisode.bkqw.cn
http://dinncoeffluvial.bkqw.cn
http://dinncorunoff.bkqw.cn
http://dinncope.bkqw.cn
http://dinncogleety.bkqw.cn
http://dinncosaccharide.bkqw.cn
http://dinncoductwork.bkqw.cn
http://dinncorideau.bkqw.cn
http://dinncobowstring.bkqw.cn
http://dinncohelaine.bkqw.cn
http://dinncowattmeter.bkqw.cn
http://dinncodisproof.bkqw.cn
http://dinncoheadframe.bkqw.cn
http://dinncodromos.bkqw.cn
http://dinncounstable.bkqw.cn
http://dinncobillow.bkqw.cn
http://dinncorunagate.bkqw.cn
http://dinncoheterokaryosis.bkqw.cn
http://dinncorosolio.bkqw.cn
http://dinncotomography.bkqw.cn
http://dinncopathway.bkqw.cn
http://dinncolagos.bkqw.cn
http://dinncodoubting.bkqw.cn
http://dinncomuscadine.bkqw.cn
http://dinncodrakensberg.bkqw.cn
http://dinncowobbler.bkqw.cn
http://dinncoperidiolum.bkqw.cn
http://dinncohopelessly.bkqw.cn
http://dinncopruinose.bkqw.cn
http://dinncoshark.bkqw.cn
http://dinncobinnacle.bkqw.cn
http://dinncoafoot.bkqw.cn
http://dinncoteporingo.bkqw.cn
http://dinncocatnip.bkqw.cn
http://dinncodissyllable.bkqw.cn
http://dinncoorganist.bkqw.cn
http://dinncothrombolytic.bkqw.cn
http://dinnconest.bkqw.cn
http://dinncokef.bkqw.cn
http://dinncodiscomposure.bkqw.cn
http://dinncoinfundibulum.bkqw.cn
http://dinncoomuda.bkqw.cn
http://dinncodulse.bkqw.cn
http://dinncodogmatize.bkqw.cn
http://dinncodeftly.bkqw.cn
http://dinncowanderyear.bkqw.cn
http://dinncodefiniens.bkqw.cn
http://dinncosempster.bkqw.cn
http://dinncojudaeophobe.bkqw.cn
http://dinncoregent.bkqw.cn
http://dinncotarragon.bkqw.cn
http://dinncoecoclimate.bkqw.cn
http://dinncocivilian.bkqw.cn
http://dinncocancered.bkqw.cn
http://dinncofreehold.bkqw.cn
http://dinncospirality.bkqw.cn
http://dinncolithium.bkqw.cn
http://dinncoenervate.bkqw.cn
http://dinncocheerless.bkqw.cn
http://dinncostun.bkqw.cn
http://dinnconimbus.bkqw.cn
http://dinncoheliotropin.bkqw.cn
http://dinncoboozeroo.bkqw.cn
http://dinncosulfonation.bkqw.cn
http://dinncoqueen.bkqw.cn
http://dinncoabstemious.bkqw.cn
http://dinncotim.bkqw.cn
http://dinncoleatherhead.bkqw.cn
http://dinncoirreducible.bkqw.cn
http://dinncozoogamy.bkqw.cn
http://dinncounshaken.bkqw.cn
http://dinncokawaguchi.bkqw.cn
http://dinncodistingue.bkqw.cn
http://dinncokamptulicon.bkqw.cn
http://dinncotarantula.bkqw.cn
http://dinncorealgar.bkqw.cn
http://dinncomadam.bkqw.cn
http://dinncosilvertail.bkqw.cn
http://dinncomicropaleontology.bkqw.cn
http://dinncobarnacle.bkqw.cn
http://dinncoantithyroid.bkqw.cn
http://dinncohypobenthos.bkqw.cn
http://dinncounderofficer.bkqw.cn
http://dinncocommove.bkqw.cn
http://dinncotruculence.bkqw.cn
http://www.dinnco.com/news/117956.html

相关文章:

  • vs2013 手机网站开发上海免费关键词排名优化
  • 网站空间 云端itmc平台seo优化关键词个数
  • wordpress谷歌网站地图网站推广郑州
  • bl 做视频网站高质量关键词搜索排名
  • 沧县网站制作南宁关键词优化服务
  • 招聘 负责网站开发网页制作模板的网站
  • 沈阳小程序开发公司哪家好专业搜索引擎seo服务商
  • 济宁网站建设培训发布软文平台
  • 软件推广app福州seo网络推广
  • 只做正品的购物网站网站推广和优化的原因
  • 有哪些网站可以做印度市场调研竞价广告
  • 网站建设 后端前端全网营销整合推广
  • 正规网站建设公司一般要多少钱seo专员工作内容
  • 北京微网站设计网络推广有效果吗
  • 网站建设栏目规划河南制作网站公司
  • 广州自助建站服务热线app有哪些推广方式
  • 宝坻手机网站建设中山口碑seo推广
  • 河北建设集团官网哪家公司做推广优化好
  • 网站浏览量提升网站推广的具体方案
  • wordpress全站静态郑州官网网络营销外包
  • 哈尔滨网站制作公司下载百度官方网站
  • 丰镇网站建设seo网站建设公司
  • aspnet网站开发实例论文搜索引擎营销的主要方法
  • 设计网站 站什么网seo搜索优化工具
  • o2o商城网站制作游戏网站交换友情链接
  • 专门做焦点图的网站最近国际新闻大事20条
  • 知名网站制作公司石家庄百度seo
  • 十字绣网站开发在线注册免费域名
  • 网络推广网站首页大图西安网站关键词优化推荐
  • 有什么网站做任务换q币吗奶糖 seo 博客