leetcode 【 Reverse Nodes in k-Group 】 python 实现
原题:
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
代码:oj测试通过 Runtime: 125 ms
# 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):
if head is None or head.next is None or k<2:
return head dummyhead = ListNode(0)
dummyhead.next = head slow = dummyhead
fast = dummyhead while 1:
sign = 0
for i in range(k):
if fast.next is not None:
fast = fast.next
else:
sign = 1
break
if sign == 1 :
break
else:
curr = slow.next
for i in range(k-1):
tmp = curr.next
curr.next = tmp.next
tmp.next = slow.next
slow.next = tmp
slow = curr
fast = curr return dummyhead.next
思路:
这道题看主要有两个点:
1. 快慢指针技巧:fast指针在前面探路,如果不满足翻转的条件,退出直接返回;slow指针在后面跟着,slow.next始终指向待翻转的第一个ListNode位置
2. 链表翻转:这个我在之前的一篇日志中已经说明了,用了一个书本倒叙的例子,详情见http://www.cnblogs.com/xbf9xbf/p/4212159.html这篇日志。
总的思路就是把看似复杂的任务分解成小任务,然后逐个击破。
leetcode 【 Reverse Nodes in k-Group 】 python 实现的更多相关文章
- [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 ...
- 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: Reverse Nodes in k-Group 解题报告
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- [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 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 ...
- leetcode Reverse Nodes in k-Group python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表
题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接 ...
- Leetcode 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 ...
- [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 ...
随机推荐
- Eucalyptus-NC管理
1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- pysnmp使用
install yum install python-pysnmp yum install python-pyasn1 or pip install pysnmp pip install pyasn1 ...
- Python对Excel操作详解
Python对Excel操作详解 文档摘要: 本文档主要介绍如何通过python对office excel进行读写操作,使用了xlrd.xlwt和xlutils模块.另外还演示了如何通过Tcl ...
- iOS开发资料
https://github.com/XCGit/awesome-objc-frameworks https://github.com/KevinHM/ios-good-practices-the-l ...
- Python变量状态保持四种方法
Python状态保持 全局 global def tester(start): global state state = start def nested(label): global state ...
- 比特币中P2PKH(pay-to-public-key-hash)的锁定脚本和解锁脚本
脚本格式 P2PKH的锁定脚本为: OP_DUP OP_HASH160 PUSHDATA(<Cafe Public Key Hash>) OP_EQUALVERIFY OP_CHECKSI ...
- PHP数组排序方法总结
随着PHP的快速发展,用它的人越来越多,在PHP数组学习摘录部分了解到最基本的PHP数组的建立和数组元素的显示.需要深入学习下PHP数组的相关操作.首先接触的就是PHP数组排序.降序的排序问题. so ...
- javaweb基础(33)_jdbc的crud操作
一.statement对象介绍 Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的exe ...
- .NET AJAX实例
引用地址:http://blog.csdn.net/qianjiu/article/details/7524228 5.2 Ajax基础http://book.csdn.net/bookfiles/6 ...
- react属性校验
https://reactjs.org/docs/typechecking-with-proptypes.html 1.安装:cnpm i prop-types -S import PropTypes ...