题目:

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

题意:给定一个链表和一个数字k,以k为循环节,反转链表中的元素,如果剩下的长度不足k,则最后一部分不进行反转。

先计算链表的长度,长度除以循环节长度,统计要进行链表反转的次数。

每个循环节中的链表反转用三个指针去维护。

当操作是第一个循环节时,需要改变head指针的值。

之后每个循环节反转结束后用ph和pe两个指针去维护循环节之间的指针连接。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
ListNode *p1,*p2,*p3,*ph,*pe;
if(head==NULL)return NULL;
int n=0,i,j;
p1=head;
if(k==1)return head;
while(p1) {
n++;
p1=p1->next;
}
p1=head;
p2=p1->next;
if(p2)p3=p2->next;
for(i=0;i<n/k;++i) {
for(j=1;j<k;++j) {
p2->next=p1;
p1=p2;
p2=p3;
if(p3)p3=p3->next;
}
if(i==0) {
ph=head;
ph->next=p2;
pe=p2;
head=p1;
}
else {
ph->next=p1;
ph=pe;
ph->next=p2;
pe=p2;
}
p1=p2;
if(p2)p2=p2->next;
if(p3)p3=p3->next;
}
return head;
}
};
// blog.csdn.net/havenoidea

题解目录

leetcode:Reverse Nodes in k-Group(以k为循环节反转链表)【面试算法题】的更多相关文章

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

  3. 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. k  ...

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

  5. 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. If ...

  6. 合并K个有序数组(链表)【字节跳动面试算法题】

    本题是本人字节跳动一面考的算法题原题是有序数组,一时没想到怎么解决数组的问题,但是如果给的是有序链表数组,则可以用下面的方法解决 可以利用最小堆完成,时间复杂度是O(nklogk),具体过程如下: 创 ...

  7. LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表

    题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接 ...

  8. leetcode Reverse Nodes in k-Group python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  9. leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

随机推荐

  1. python3.4.3如何获取文件的路径

    #coding:utf-8from tkinter import *from tkinter import filedialogroot = Tk()root.filename = filedialo ...

  2. 高质量程序设计指南C/C++语言——C++/C编译预处理

    C++/C的编译预处理器对预编译伪指令进行处理后生成中间文件作为编译器的输入,因此所有的预编译伪指令都不会进入编译阶段.预编译伪指令一般都以#打头,且其前面只能出现空白字符.预编译伪指令不是C++/C ...

  3. 【c语言】求最大值

    一.我个人觉得求最大值比较简单的一种方法(当然同时求最大值和最小值时稍微改改也能行) #include <stdio.h> int main(void) { int f, i, max; ...

  4. octopress command memo

    1 rake new_post rake new_post[title]           # Begin a new post in source/_posts 2 rake preview ht ...

  5. Oracle Database 12c Release 1 Installation On Oracle Linux 6.4 x86_64

    Create groups and users [root@vmdb12c ~]# groupadd oinstall [root@vmdb12c ~]# groupadd dba [root@vmd ...

  6. 开机时进入 grub rescue>的解决方法

    本机是centOS7和win8的双系统 之前在win上把一个空的磁盘空间释放了 可能造成了grub的一些问题 具体还没有研究过 开机后无法正常进入grub引导画面 而是跳出一串英文+ grub res ...

  7. FastStone Capture(FSCapture) 注册码 _图形图像_软件教程_脚本之家

    FastStone Capture(FSCapture) 注册码 _图形图像_软件教程_脚本之家 FastStone Capture 注册码 序列号: name/用户名:TEAM JiOO key/注 ...

  8. iOS KVO & KVC

    键值观察:值更改时通知观察者 键值观察(Key-value observing,或简称 KVO)允许对象观察另一个对象的属性.该属性值改变时,会通知观察对象.它了解新值以及旧值:如果观察的属性为对多的 ...

  9. apache2.4.x三种MPM介绍

    三种MPM介绍                                                                               Apache 2.X  支持 ...

  10. windowsphone中获取手机位置信息

    首先在界面中加入一个textblock控件以显示信息 using System; using System.Collections.Generic; using System.IO; using Sy ...