Reverse atnodess in k-Group

问题描述如下:
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
主要考察指针的操作,在纸上画个例子,细心点就好了:
下面是代码:注意pKth指的是第K个节点, pFirst 到 pKth 是需要反转的子链:
* Definition for singly-linked list.
* struct ListNode {
ListNode *
ListNode(int x) : val(x), next(NULL) {}
#define LN ListNode*
class Solution {
LN reverseKGroup(LN head ,int k)
if (!head||!head-&next||k&=1)
LN pFirst=
LN pBefore=NULL;
LN pKth=pF
int cnt=k-1;
while(cnt&&pKth)
pKth=pKth-&
if ( cnt&0 || pKth==NULL)
reverse(pBefore,pFirst,pKth);
if (pBefore==NULL)
pBefore=pK
pFirst=pKth-&
void reverse(LN pBefore,LN& pFirst,LN& pKth)
while(pN!=pKth)
LN pTmp=pN-&
pN-&next=pKth-&
pKth-&next=pN;
LN pTmp=pF
if (pBefore)
pBefore-&next=pF
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:95995次
积分:3101
积分:3101
排名:第4244名
原创:215篇
转载:29篇
评论:23条
(2)(33)(46)(43)(33)(16)(32)(19)(9)(1)(11)(1)python-cn(华蟒用户组,CPyUG 邮件列表)上:&&相关讨论
本机环境: 2核CPU, linux 2.6, python 2.6.2
&&在python上开启多个线程,由于GIL的存在,每个单独线程都会在竞争到GIL后才
运行,这样就干预OS内部的进程(线程)调度,结果在多核CPU上:
&&python的多线程实际是串行执行的,并不会同一时间多个线程分布在多个CPU上运行。
&&但是这里有个有趣的现象: python开启两个死循环的线程,在我的2核机器上会有如下
CPU使用情况,每个CPU都维持在50%左右的使用率(见下图).
难道python的多线程可以在多核上并行?
&&当然不是,在GIL存在的python上,多线程应该是不可能并行的。
&&这里其实有个小细节隐藏在linux的进程调度系统内,当python多线程切换时候,linux
调度子系统会兼顾多核的负载情况,linux会把多个线程平均分布在多核上跑,这样不
会导致单个核负载过盛。但是这个并不会让python多线程在多核上并行。
大致CPU使用应该这样:
&&cpu0: .. pythread0 _________ pythread0 _________ ..
&&cpu1: .. _________ pythread1 _________ pythread1 ..
&&('___'表示cpu空闲,这样统计下来每个核使用情况在50%左右)
&&如果python多线程真能在多核上并行,那么这里每个CPU使用率都应该接近100%.
原文链接:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:40578次
排名:千里之外
原创:45篇
转载:15篇
评论:51条
文章:30篇
阅读:4359[Leetcode] Reverse Nodes in k-Group - coolgo,just go! - ITeye技术网站
博客分类:
Reverse Nodes in k-Group3953 / 13303
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 {
ListNode *
ListNode(int x) : val(x), next(NULL) {}
class Solution {
ListNode *reverseKGroup(ListNode *head, int k) {
if (k == 1)
ListNode *start, *end, *prev, *next, *my_
start = end =
my_head = new ListNode(123);
my_head-&next =
prev = my_
while (start != NULL) {
int t = 1;
while (end != NULL && t & k) {
end = end-&
if (t == k && end != NULL) {
next = end-&
reverse(start, end, prev, next);
start = end = prev-&
ListNode* res = my_head-&
delete my_
void reverse(ListNode* s, ListNode* e, ListNode* p, ListNode* n) {
ListNode *cur = s, *prev = NULL, *
while (cur != n) {
next = cur-&
cur-&next =
* Definition for singly-linked list.
* struct ListNode {
ListNode *
ListNode(int x) : val(x), next(NULL) {}
class Solution {
ListNode *reverseKGroup(ListNode *head, int k) {
if (head == NULL)
ListNode *s, *e, *next, *prev, myhead(99);
myhead.next =
while (s != NULL) {
while (e != NULL && cnt & k) e = e-&next, cnt++;
if (cnt & k || e == NULL)
next = e-&
reverse(s, e);
prev-&next =
return myhead.
void reverse(ListNode* head, ListNode* tail) {
ListNode* prev = NULL, *cur = head, *
while (prev != tail) {
next = cur-&
cur-&next =
论坛回复 /
浏览: 23443 次
来自: 南京1148人阅读
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
原题链接:/problems/reverse-nodes-in-k-group/
题目:给定一个链表,一次反转链表的k个值,并返回逆转后的结果链表。如果节点的数量不是k的倍数,那么保留原来的样子。
思路:如果将链表转成数组来做的话,就更加方便了。但是这里还是用链表来逆转,其实思路是一样的,遇到k个长度就将这k个节点逆转就可以了。
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k &= 1)
ListNode dummy = new ListNode(-1);
dummy.next =
int count = 0;
ListNode pre = dummy,current =
while (current != null) {
ListNode post = current.
if(count == k){
pre = reverse(pre,post);
count = 0;
return dummy.
ListNode reverse(ListNode pre,ListNode post){
ListNode dummy = pre.
ListNode current = dummy.
while(current != post){
ListNode next = current.
current.next = pre.
pre.next =
dummy.next =
// Definition for singly-linked list.
public class ListNode {
ListNode(int x) {
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:234955次
积分:4459
积分:4459
排名:第2360名
原创:191篇
译文:25篇
评论:20条
(3)(2)(24)(4)(8)(31)(46)(30)(16)(27)(18)(10)(2)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-&5For&k&= 2, you should return:&2-&1-&4-&3-&5For&k&= 3, you should return:&3-&2-&1-&4-&5思路分析:这题属于反转单链表的变形题目,首先我们要搞清楚如何反转单链表,比较好写的方法是定义pre,cur,after三个指针,我们要做的就是iter链表,试图把每个当前的cur node都“插入”到链表“头部”之前,为了方便操作,我们维护pre,cur,after三个指针,同时用dummy来维护新的头结点,这样可以方便我们不断向“头部”之前插入新的node。如下函数可以反转任意给定两个node之间的链表(不包含这两个node),返回被反转部分的第一个node。注意实现这个函数时,要保证反转之后两端仍然与beginNode和endNode正确链接,最后那句beginNode.next = dummy就是链接beginNode和反转之后新的头结点。这个bug浪费了不少时间,以后要更加仔细。//reverse nodes between beginNode and endNode(exclusively)
//return the first node in the reversed part
private static ListNode reverseLinkedList(ListNode beginNode, ListNode endNode) {
// TODO Auto-generated method stub
ListNode head = beginNode.
ListNode dummy =//use dummy to maintain the new head
ListNode pre =
ListNode cur = pre.
ListNode after = cur.
while(cur != endNode){
pre.next =
cur.next =
cur = pre.
if(cur == null)
after = cur.
beginNode.next =//!after reverse, beginNode should also before the first Node, endNode should also before the last node
}然后我们就可以调用这个函数来解决本题,需要定义一个fakeHead,其next成员是真正的head,作为初始的beginNode。我们iter链表,每经过k个node做一次反转操作,调用上面的函数,要注意传入正确的beginNode和endNode,同时注意更新head,返回反转完成之后新的head。时间复杂度O(N)。AC Code/**
* Definition for singly-linked list.
* public class ListNode {
ListNode(int x) {
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null || head.next == null || k & 2)
ListNode fakeHead = new ListNode(-1);
fakeHead.next =
ListNode pre = fakeH
ListNode cur =
int i = 0;
while(cur != null){
cur = cur.
if(i % k == 0){
pre = reverseLinkedList(pre, cur);
if(i == k){
int temp =
while(temp & 1){
pre = pre.
//reverse nodes between beginNode and endNode(exclusively)
//return the first node in the reversed part
private static ListNode reverseLinkedList(ListNode beginNode, ListNode endNode) {
// TODO Auto-generated method stub
ListNode head = beginNode.
ListNode dummy =//use dummy to maintain the new head
ListNode pre =
ListNode cur = pre.
ListNode after = cur.
while(cur != endNode){
pre.next =
cur.next =
cur = pre.
if(cur == null)
after = cur.
beginNode.next =//!after reverse, beginNode should also before the first Node, endNode should also before the last node
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:423821次
积分:6146
积分:6146
排名:第1328名
原创:178篇
转载:32篇
评论:572条
(25)(2)(13)(7)(2)(1)(1)(1)(1)(1)(4)(1)(6)(3)(2)(2)(4)(9)(11)(5)(24)(17)(1)(27)(36)(1)(2)(2)}

我要回帖

更多关于 nodes.dat 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信