Question

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

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

Solution

这道题并不难,但是比较繁琐。思路是两步走:

1. 找出当前要反转的子序列

2. 反转子序列 (类比反转 m - n 那道题)

对要反转的子序列,我们用一头(prev)一尾(end)表示

prev -> 1 -> 2 -> 3 -> 4 -> end

如k=4,在help函数中,我们定义cur = prev.next, then = cur.next 循环结束条件是then != end。最后返回新序列的尾指针,即下一个要反转序列的prev指针,该例子中为4。

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k == 0 || k == 1) {
return head;
}
ListNode dummy = new ListNode(-1);
ListNode prev = dummy;
dummy.next = head;
int i = 0;
while (head != null) {
i++;
if (i % k == 0) {
prev = reverse(prev, head.next);
head = prev.next;
} else {
head = head.next;
}
}
return dummy.next;
} private ListNode reverse(ListNode prev, ListNode end) {
ListNode cur = prev.next, head = prev.next;
ListNode then = cur.next;
cur.next = null;
ListNode tmp;
while (then != end) {
tmp = then.next;
then.next = cur;
cur = then;
then = tmp;
}
prev.next = cur;
head.next = end;
return head;
}
}

Reverse Nodes in k-Group 解答的更多相关文章

  1. [Leetcode] 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. If ...

  2. Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转

    问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...

  3. 【Reverse Nodes in k-Group】cpp

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

  4. [LintCode] 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. If ...

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

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

  6. 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划分 ...

  7. LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]

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

  8. LeetCode: Reverse Nodes in k-Group 解题报告

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

  9. 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 ...

  10. Leetcode 25/24 - Reverse Nodes in k-Group

    题目描述 Leetcode 24 题主要考察的链表的反转,而 25 题是 24 的拓展版,加上对递归的考察. 对题目做一下概述: 提供一个链表,给定一个正整数 k, 每 k 个节点一组进行翻转,最后返 ...

随机推荐

  1. global.asax?app.config?webconfig??

    一.Global.asax 1.global.asax是什么? 一个文本文件,至于它包含写什么内容?顾名思义,global 肯定是掌管一个应用程序(application)的全局性的东西,例如应用程序 ...

  2. c语言输出可见字符

    #include <stdio.h> void main() { int i; //可见字符是32-126 ;i<;i++) { putchar(i); } getchar(); }

  3. pyqt颜色字符

    from PyQt4.QtGui import QPlainTextEdit, QWidget, QVBoxLayout, QApplication, \ QFileDialog, QMessageB ...

  4. NSTimer 详细设置

    NSTimer 详细设置1:http://blog.csdn.net/davidsph/article/details/7899483 NSTimer 详细设置2:http://blog.csdn.n ...

  5. 畅通project

    原文请訪问:p=174">http://xiaoshig.sinaapp.com/?p=174 畅通project Time Limit:2000MS     Memory Limit ...

  6. Eclipse 将projectBuild Path中引用的jar包自己主动复制到WEB-INF下的lib目录下

    在用用 Eclipse进行Java Web开发时,web应用中引用的jar须要复制到WEB-INF下的lib目录下,否则常常出现ClassNotFound异常. 通过以下方法,能够不用手动拷贝jar包 ...

  7. UITableView滑动按钮的操作

    一.开题  首先先创建工程, 创建工程的步骤就不一一介绍了, 前面有提过, 接下来是要在VC上创建一个tableview当然你也可以选择一个类继承于UITableView两者都可以, 这要看个人喜欢了 ...

  8. compass模块

    Compass核心模块Reset :重置CSS模块 @import "compass/reset" Layout :页面布局的控制能力 @import "compass/ ...

  9. ajax例子

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  10. FineUI按钮控件

    按钮的状态与大小 按钮有启用/禁用,按下/正常几种状态,对应的属性分别为Enabled.EnablePress.Pressed三个属性. 按钮有大中小三个尺寸,对应的属性为Size. 按钮上的图标 按 ...