# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Group
https://oj.leetcode.com/problems/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 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 ===Comments by Dabay===
前面加一个辅助head。
用一个栈来记录k个元素,当栈不满的时候入栈,当栈大小到k的时候处理这k个元素。
最后检查栈是否为空
如果为空,末尾加None
如果不为空,末尾指向栈底的元素即可(因为入栈的时候,并没有破坏链表顺序)
''' # Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
# @param head, a ListNode
# @param k, an integer
# @return a ListNode
def reverseKGroup(self, head, k):
previous = new_head = ListNode(0)
node = new_head.next = head
stack = []
while node:
stack.append(node)
node = node.next
if len(stack) == k:
while len(stack) > 0:
pop_node = stack.pop()
previous.next = pop_node
previous = pop_node
else:
if len(stack) > 0:
previous.next = stack[0]
else:
previous.next = None
return new_head.next def print_listnode(node):
while node:
print "%s->" % node.val,
node = node.next
print "END" def main():
sol = Solution()
node = root = ListNode(1)
for i in xrange(2, 3):
node.next = ListNode(i)
node = node.next
print_listnode(root)
print_listnode(sol.reverseKGroup(root, 2)) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]25: 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. 【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 ...

  3. 【一天一道LeetCode】#25. Reverse Nodes in k-Group

    一天一道LeetCode系列 (一)题目 Given a linked list, reverse the nodes of a linked list k at a time and return ...

  4. 【LeetCode】25. 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. k  ...

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

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

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

  7. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

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

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

  9. [LeetCode] 25. 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  ...

随机推荐

  1. 转载 java枚举类型enum的使用 (原文地址:http://blog.csdn.net/wgw335363240/article/details/6359614)

    java枚举类型enum的使用 最近跟同事讨论问题的时候,突然同事提到我们为什么java中定义的常量值不采用enmu枚举类型,而采用public final static 类型来定义呢?以前我们都是采 ...

  2. c语言函数实现交换两个数的值

    代码: #include <stdio.h> void swap(int x,int y) { int temp; temp = x; x = y; y = temp; printf(&q ...

  3. c# 中的 Trim

    1. 让用户输入字符串 并且判断是否是 'yes'(无关大小写) Console.WriteLine("input a string"); string userResponse ...

  4. Linux Syslog

    http://blog.chinaunix.net/uid-677314-id-164215.html http://blog.chinaunix.net/uid-24708340-id-397132 ...

  5. Memcached管理与监控工具 memAdmin

    http://www.junopen.com/memadmin/ 使用MemCached以后,肯定希望知道cache的效果,对于MemCached的一些运行状态进行监控是必要的,memcached提供 ...

  6. docker 创建镜像

    docker:/root# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official bui ...

  7. 只有电信3G是公网ip。

    只有电信3G是公网ip,其它网络拿到是内部网.

  8. iOS多线程总结(一)NSThread

    多线程,简而言之,就是提供代码的多个执行路径,对于App性能和用户体验都有着至关重要的意义.在iOS开发中,Apple提供了不同的技术支持多线程编程,主要有NSThread.NSOperationQu ...

  9. Unable to open ...\tools\capture\allegro.cfg for reading

    采用Capture CIS 当生成网表.误: Unable to open ...\tools\capture\allegro.cfg for reading. Please correct the ...

  10. 如何更快速加载你的JS页面

    确保代码尽量简洁 不要什么都依赖JavaScript.不要编写重复性的脚本.要把JavaScript当作糖果工具,只是起到美化作用.别给你的网站添加大量的JavaScript代码.只有必要的时候用一下 ...