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

▶ 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. HDU 1896:Stones(优先队列)

    Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Sub ...

  2. HDU 3746:Cyclic Nacklace(KMP循环节)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. (转)C++ 容器及选用总结

    目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭代器 第五 ...

  4. 【spring源码分析】spring AspectJ的Execution表达式

    在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execution (* com.sam ...

  5. python 2个dict如何合并

    dictMerged2 = dict( dict1, **dict2 ) 这种效率比较高 refer to: http://www.pythoner.com/13.html

  6. WPF 第三方控件

    目前第三方控件在网上形成巨大的共享资源,其中包括收费的也有免费的,有开源的也有不开源的,合理的使用第三方控件将使项目组的工作事半功倍.比如项目中有些复杂的业务逻辑.有些绚丽的效果需要有专门的定制控件才 ...

  7. Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧)

    干货:Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧) [解决办法]: 菜单栏中[File]->[Reload As E ...

  8. 【转】解决Win7字体模糊不清晰的最佳办法

    原文网址:http://blog.sina.com.cn/s/blog_3d5f68cd0100ldtp.html 相信初次用win7的朋友,都会遇到字体不清晰的问题,有很多人因为这个问题而放弃使用w ...

  9. RAC3——RAC原理开始

    1.RAC并发 RAC的本质是一个数据库,只不过现在这个数据库运行在了多台计算机上,在原先的单实例中,一个进程是否可以修改一条数据,取决于是否有其他进程(同一台计算机上)并发修改.在RAC环境下,这种 ...

  10. 常用Web框架

    1.Aliceui Aliceui是支付宝的样式解决方案,是一套精选的基于 spm 生态圈的样式模块集合,是 Arale 的子集,也是一套模块化的样式命名和组织规范,是写 CSS 的更好方式. git ...