# -*- 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. PHP框架学习之Laravel安装

    自从接触PHP以来一直使用Yii,感觉Yii实现功能比较简单,是一个很不错的框架.最近由于工作的原因开始研究Laravel5,不得不说我在第一步安装就被坑着了,下面就是我痛苦的学习成果.Laravel ...

  2. linux下实现自己的shell解释器

    实现一个自己的shell解释器,其原理比较简单,首先获取用户的输入,通过fork()函数获取两个进程(父子进程),子进程通过execvp()函数继续进行,此时父进程一直在等待子进程的结束,待都结束了就 ...

  3. 迁移 Qt4 至 Qt5 的几个主要环节(数据库插件别拷错了地方)

    Qt5推出一段时间了,经过了试用,虽然还存在一些问题,比如Designer 缺少 WebView 和 ActiveQt 的UI工具,此外 WebKit 的 Release 版本似乎和Visual-St ...

  4. TCP/IP Checksum 吐槽

    算法原理: 假定 output[2] 为输出结果,input[n]为待计算校验和的内存块. 1)所有奇数位[0,2,4……] byte 累加进 结果的奇数位内存 output[0],如果溢出,则进位给 ...

  5. nodejs递归创建目录,同步和异步方法

    nodejs递归创建目录,同步和异步方法.在官方API中只提供了最基本的方法,只能创建单级目录,如果要创建一个多级的目录(./aaa/bbb/ccc)就只能一级一级的创建,感觉不是很方便,因此简单写了 ...

  6. Android TextView文字描边的实现!!

    Android开发:文字描边 转自:http://www.oschina.net/code/snippet_586849_37287 1. [代码][Java]代码 1 2 3 4 5 6 7 8 9 ...

  7. ChartControl第一课简短的控件初步设计

    WinForms Controls >Controls > Chart Control > Getting Started This document gives you a qui ...

  8. 网页平面设计 HTML

    网页平面设计HTML基础 1.网页的基本元素:文字.图像.超链接 2.HTML的基本机构head.title.body三部分 <html> <head> <title&g ...

  9. sql 去除结尾的回车或者换行

    CREATE FUNCTION REMOVE_ENTER (@DESC VARCHAR(8000))RETURNS VARCHAR(8000)ASBEGIN DECLARE @STR VARCHAR( ...

  10. UIView 属性

    1.alpha 设置视图的透明度.默认为1. ojbc // 完全透明 view.alpha = 0; // 不透明 view.alpha = 1; 2.clipsToBounds 默认是NO,当设置 ...