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

  

/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(k <= || head == NULL) return head;
int len = , tempK;
ListNode *p, *cur,*q , *pre ,*tail; q = head;
while(q){
len++;
q = q->next;
}
if(len < k) return head; cur = head;
pre = NULL;
while(len >= k){ tempK = ; p = NULL;
while( tempK < k){
if(p == NULL){
p = cur;
tail = cur;
cur = cur->next;
}else{
q = cur->next;
cur->next = p;
p = cur;
cur = q;
}
tempK++;
} if(pre == NULL)
head = p;
else
pre ->next = p;
pre = tail;
pre->next = cur;
len = len - k;
} return head;
}
};

LeetCode_Reverse Nodes in k-Group的更多相关文章

  1. 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 算法思想:基本操作就是链表 ...

  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】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  4. [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  5. [Swift]LeetCode863. 二叉树中所有距离为 K 的结点 | All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a targetnode, and an integer value K. Return a lis ...

  6. 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

    [抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...

  7. LeetCode – All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  8. [Leetcode] 863. All Nodes Distance K in Binary Tree_ Medium tag: BFS, Amazon

    We are given a binary tree (with root node root), a target node, and an integer value `K`. Return a ...

  9. leetcode 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

随机推荐

  1. Linux中.a,.la,.o,.so文件的意义和编程实现

    Linux中.a,.la,.o,.so文件的意义和编程实现    Linux下文件的类型是不依赖于其后缀名的,但一般来讲:        .o,是目标文件,相当于windows中的.obj文件     ...

  2. HDU3727--Jewel (主席树 静态区间第k大)

    Jewel Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. 《SDN核心技术剖析和实战指南》第一章小结

    第一章主要是概况.新技术有一个特点是,每家都有不同的说法.这里我只说说我比较认同的部分. SDN的核心概念大概有两个:转发面与控制面分离.开发可编程化.书里还说逻辑上集中控制,其实这个就可以从转发与控 ...

  4. linux中cat、more、less、tail、head命令

    cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此经常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面在显示满一页时暂停,此时可按空格健继 ...

  5. ORACLE STUDY NOTES 01

    [JSU]LJDragon's Oracle course notes In the first semester, junior year DML数据操纵语言 DML指:update,delete, ...

  6. wpf在异步中给前台赋值

    wpf,新建异步方法: Thread newThread = new Thread(new ParameterizedThreadStart(GetResult)); newThread.Start( ...

  7. Java学习之List接口

    List接口 List接口的定义如下: public interface List<E>extends Collection<E> 可以发现List接口时Collection接 ...

  8. 公告:CSDN博客频道新功能正式上线!

    各位尊敬的CSDN用户: 你们好! 为了更好的服务于用户,CSDN博客最新推出如下功能: 1.取消开通博客3天才能发布博文的限制,博客开通之后即可发表博文 2.博客文章增加自定义摘要功能    在发表 ...

  9. mybatis之特殊查询

    在mybatis查询的过程中,某个字段是经过计算得到的,这时,在设计数据表的时候,就不 必在增加此对应的字段 那么,在查询的时候,页面有需要展示这个字段时,怎么办呢? 举个例子: 在查询微信团商品时, ...

  10. Java初转型-jdk安装和配置

    Java 开发环境配置 > * 下载JDK> * 配置环境变量> * 测试JDK是否安装成功> * 使用 Eclipse 运行第一个 Java 程序 下载JDK 首先我们需要下 ...