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

▶ 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. HUD 1969:Pie(二分)

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  2. 线上服务内存OOM问题定位[转自58沈剑]

    相信大家都有感触,线上服务内存OOM的问题,是最难定位的问题,不过归根结底,最常见的原因: 本身资源不够 申请的太多 资源耗尽 58到家架构部,运维部,58速运技术部联合进行了一次线上服务内存OOM问 ...

  3. java新的语法糖:Java 8 Lambda表达式

    ***************************************************************************

  4. 使用 phpStudy + VSCODE 进行 PHP 断点调试

    使用 phpStudy + VSCODE 进行 PHP 断点调试 自己摸索过程有点曲折,但还是配置成功了,现分享如下. 原料 phpStudy 2018 VSCODE 配置过程 安装 phpStudy ...

  5. 【转】每天一个linux命令(42):kill命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/12/20/2825837.html Linux中的kill命令用来终止指定的进程(terminate a ...

  6. protobuf 协议 windows 下 C++ 环境搭建

    1. 下载protobuf https://code.google.com/p/protobuf/downloads/list Protocol Buffers 2.5.0 full source - ...

  7. php语言介绍分析

    1,胡说八道 php设计专门用于web开发的编程语言,易学易用得到广泛应用的同时也饱受诟病,简单易学使得初学者用最短的时间很容易实现自己的WEB站点,且开源项目丰富,是中小型公司热衷的选择.但是,随着 ...

  8. js和jquery判断该元素中是否有指定class

    <div class="test">test</div> var t = document.getElementsByClassName('test'); ...

  9. [转]NSIS:安装、卸载时检查程序是否正在运行

    原文链接 http://www.flighty.cn/html/bushu/20110402_115.html 如果我们要安装或升级的程序正在运行,文件肯定会替换失败,以下代码可以提示用户结束正在运行 ...

  10. 1050 String Subtraction (20 分)

    1050 String Subtraction (20 分) Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be the ...