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个swap,需要再做一次reverse,使之成为正序。

指针赋值时注意前一个右项作为后一个左向,一一赋值。

/**
* 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* dummyHead = new ListNode();
dummyHead->next = head;
ListNode* current = head; //the node to do swap
ListNode* subHead = dummyHead; //the node before the head of the group
ListNode* subTail; //the last node of the group while(current && current->next){
subTail = current;
current = current->next;
for(int i = ; i < k; i++){ //k group needs k-1 swap
if(current==NULL){
//reset: do once more reverse
subTail = subHead->next;
current = subTail->next;
for(int j = ; j < i; j++){
subTail->next = current->next;
current->next = subHead->next;
subHead->next = current;
current = subTail->next;
}
break;
} //assign value one by one
subTail->next = current->next;
current->next = subHead->next;
subHead->next = current;
current = subTail->next;
}
subHead = subTail;
} return dummyHead->next;
}
};

25.Reverse Nodes in k-Group (List)的更多相关文章

  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. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

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

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

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

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

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

  8. 蜗牛慢慢爬 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. ...

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

随机推荐

  1. New Concept English Two 28 76

    $课文74  舞台之外 784. An ancient bus stopped by a dry river bed and a party of famous actors and actresse ...

  2. 从JDK源码角度看Short

    概况 Java的Short类主要的作用就是对基本类型short进行封装,提供了一些处理short类型的方法,比如short到String类型的转换方法或String类型到short类型的转换方法,当然 ...

  3. C/C++ 父子进程之间的文件描述符问题

    在C程序中,文件由文件指针或者文件描述符表示.ISO C的标准I/0库函数(fopen, fclose, fread, fwrite, fscanf, fprintf等)使用文件指针,UNIX的I/O ...

  4. CF 432D

    http://codeforces.com/problemset/problem/432/D 在前缀是后缀的前提下,求这个前缀在原串中出现了多少次 出现的次数可以用dp求解,前缀是后缀直接用Next判 ...

  5. PHP write byte array to file

    /********************************************************************************* * PHP write byte ...

  6. python线程的GIL问题(全局解释器锁)

    造成原因: python ---> 支持线程操作 --->IO的同步和互斥 --> 加锁 ----> 超级锁,给解释器加锁--->解释器同一时刻只能解释一个线程 造成的后 ...

  7. 把字符串中的空格替换为"%20"

    这个需要注意的是字符串的结尾最后一个字符为'\0',并不是空字符,复制时要一块复制,算法思想就是先计算出字符串中总的空格数,然后 重新计算字符串的长度,由于"%20"为3个字符,比 ...

  8. ES6必知必会 (六)—— Class

    1.在之前的JS面向对象编程中,如果定义一个构造函数,一般来说是这样: function Person( name , age ) { this.name = name; this.age = age ...

  9. nginx 正则

    syntax: location [=|~|~*|^~] /uri/ { … }语法:location [=|~|~*|^~] /uri/ { … } 优先级从高到低:=.~ .~*.^~.空 def ...

  10. Window 查看端口进程使用

    1. 查看所有端口占用 netstat -ano 2. 查看指定端口占用 netstat -ano | findstar "8080" 其中11152 对应为进程号 3. 查看进程 ...