▶ 问题:单链表中的元素进行交换或轮换。

▶ 24. 每两个元素进行翻转。如 [1 → 2 → 3 → 4 → 5] 变换为 [2 → 1 → 4 → 3 → 5]

● 初版代码,4 ms

 class Solution
{
public:
ListNode* swapPairs(ListNode* head)
{
int length;
ListNode newHead(-), *x, *y;
newHead.next = head; // 添加头结点,方便操作原链表的首元
for (x = head, length = ; x != nullptr; x = x->next, length++);// 计算链表元素个数
if (length <= ) // 无需交换的情形
return head;
for (x = &newHead, y = x->next; y != nullptr && y->next != nullptr; x = x->next->next, y = y->next)
{ // y 指向需要交换的两个节点的靠前一个,x 指向 y 的再前一个节点
x->next = x->next->next;
y->next = x->next->next;
x->next->next = y;
}
return newHead.next;
}
};

● 改良版代码,3 ms 。减少了结点计数,改变了操作顺序,后面 k 元轮换的算法与此类似。

 class Solution
{
public:
ListNode* swapPairs(ListNode* head)
{
if (head == nullptr || head->next == nullptr)// 0 或 1 个元素
return head;
ListNode newHead(-);
newHead.next = head;
for (ListNode *x = &newHead, *y = x->next->next;;)// y指向需要交换的两个元素的后者
{
x->next->next = x->next->next->next;
y->next = x->next;
x->next = y;
x = y->next; // 一定存在,不用检查 nullptr
if (x->next == nullptr || (y = x->next->next) == nullptr)
break;
}
return newHead.next;
}
};

▶ 25. 每 k 个元素进行翻转。如 k = 4 时,[1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9] 变换为 [4 → 3 → 2 → 1 → 8→ 7 → 6 → 5 → 9]

● 自己的代码,26 ms

 class Solution
{
public:
ListNode* reverseKGroup(ListNode* head, int k)
{
int length, front;
ListNode newHead(-), *x, *y, *z, *w;
for (x = head, length = ; x != nullptr; x = x->next, length++);// 求链表长,是否还剩 k 元可以进行轮换由 length 决定
if (length < || k < || length < k) // 不需要调整的情形
return head;
for (front = , newHead.next = head, x = &newHead; (length - front) / k;)// front 代表当前处理的元素的最大序号(w 指向的元素的编号)
{
y = x, z = y->next, w = z->next;
for (front++; front % k; front++)// 每次循环先跳转 y、z、w 建立一个链接
{
y = z, z = w, w = w->next;
z->next = y;
}
x->next->next = w; // 之后建立与 x 有关的链接
y = x->next;
x->next = z;
x = y;
}
return newHead.next;
}
};

● 大佬的代码,25 ms

 class Solution
{
public:
ListNode* reverseKGroup(ListNode* head, int k)
{
ListNode *node = head, *rev;
for (int i = ; i < k; node = node->next, i++)
{
if (node == NULL)
return head;
}
rev = reverse(head, node);
head->next = reverseKGroup(node, k);
return rev;
}
ListNode * reverse(ListNode *start, ListNode *end)
{
ListNode *prev = end, *next;
while (start != end)
{
next = start->next;
start->next = prev;
prev = start;
start = next;
}
return prev;
}
};

24. Swap Nodes in Pairs + 25. Reverse Nodes in k-Group的更多相关文章

  1. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  2. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  3. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  4. [LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k  ...

  5. 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  6. 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

  7. 25.Reverse Nodes in k-Group (List)

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  8. 25. Reverse Nodes in k-Group (JAVA)

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k  ...

  9. LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)

    题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description   Problem :将一个有序list划分 ...

随机推荐

  1. eclipse 配置jdk和maven

    准备工作:确保已安装好jdk和maven,并完全配置环境.若是没有请参考前两篇博客: jdk:    http://www.cnblogs.com/qinbb/p/6861851.html maven ...

  2. 关于递归函数中的return位置

    1.对于求是否有解的问题一般使用bool dfs()  其中return 可以放在递归式后面 2.对于需要更新解的问题一般使用int dfs()  其中return 不能放在递归式后面,必须放在函数最 ...

  3. JQuery输入框获取/失去焦点行为

    //搜索框获取焦点清除内容 $(function() { $("input").focus(function() { //获取焦点,清空默认内容 $(this).css('colo ...

  4. jaeger 使用ElasticSearch 作为后端存储

    jaeger 支持es 作为后端存储,这样对于查询.以及系统扩展是比较方便的 使用docker-compose 运行 环境准备 参考项目: https://github.com/rongfenglia ...

  5. Ribbon Status Bar

    https://documentation.devexpress.com/#WindowsForms/CustomDocument2498 官方文档说明 A Ribbon Status Bar (Ri ...

  6. TensorFlow入门教程集合

    TensorFlow入门教程之0: BigPicture&极速入门 TensorFlow入门教程之1: 基本概念以及理解 TensorFlow入门教程之2: 安装和使用 TensorFlow入 ...

  7. 完成分析 FastAdmin 用户余额功能(后台篇)

    分析 FastAdmin 用户余额功能(后台篇) 分析 FastAdmin 用户余额功能(后台篇) 虽然 FastAdmin 主要针对后台的框架,但也在不断完善前台的功能,有一天小伙伴在社区里提了一个 ...

  8. vue 项目中,定时器(setInterval)的写法

    vue 项目中,定时器(setInterval)的写法: fetchJobList是一个方法,里面有dispatch一个action进行请求接口的代码. data () { return { inte ...

  9. SQL的datetime类型数据转换为字符串格式大全

    Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE( ...

  10. Winfrom 实现转圈等待

    1.放弃进度条.动态进度图片等方式实现用户体验优化方式(主要是优化用户等待体验),建议使用方式? 答:对于From或者Control而言,其提供了Cursor属性设置即可. 例如: this.Curs ...