[Leetcode][Python]25: Reverse Nodes in k-Group
# -*- 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的更多相关文章
- [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 ...
- 【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 ...
- 【一天一道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 ...
- 【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 ...
- 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 算法思想:基本操作就是链表 ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 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 ...
- [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 ...
随机推荐
- javascript获取元素结点到页面的绝对距离的方式
var div = document.getElementById('div');var p = getPos(div); function getPos(obj) { var pos = {left ...
- PHP多线程的实现方法详解
PHP5中可以使用新增的stream_socket_client()函数直接替换掉fsocketopen().PHP5之前的版本,你需要自己动手,用sockets扩展解决问题.PHP5的先进之处在于, ...
- HTML特殊符号编码大全
HTML特殊字符编码:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全. ´ ´ © © > > µ ...
- netbean7.4 保存远程项目的时候老是跳警告框的解决方案
在任意位置新建一个空白文件,然后在 管理远程连接里面=>已知的主机文件=>点浏览就行了
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- Linux学习(一):linux更改ip地址命令_更改DNS_更改默认网关_更改子网掩码_主机名
如何使用命令来更改linux的IP .DNS .子网掩码,在虚拟机(vitrualBox)上添加一个Linux的虚拟机; 安装方法:http://pan.baidu.com/s/1sjJPhP7 安装 ...
- html表格单元格设置背景颜色
- C语言中一些非常酷的技巧(cool tricks)
来自Quora,认为不错,就实践了一下. 1. #if 0 ...... #endif 块中的内容不会被编译,由于凝视不同意嵌套,我们能够把临时不用的代码块放在 这里面. 2. 数组初始化的时候能够 ...
- Exception Handling in ASP.NET Web API
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...
- Java面试题之Request对象的主要方法
setAttribute(String name,Object):设置名字为name的request的参数值 getAttribute(String name):返回由name指定的属性值 getAt ...