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 ...
随机推荐
- Java图形界面开发—简易记事本
在学习了Java事件之后,自己写了一个极其简单的记事本.用到了MenuBar,Menu,MenuITem等控件,事件包括ActionListener以及KeyListener. 代码如下: ...
- Jquery里面的$(this)和this, 有什么区别
当你用的是jquery时,就用$(this),如果是JS,就用this $(this).html( $(this).html() + " BAM! "); 这个里的html()是J ...
- django ORM 简单示例简绍
简单 models 操作 class Host(models.Model): nid = models.AutoField(primary_key=True) #Nid为主键 hostname = m ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- python_54_函数调用函数
logger函数的定义要放在函数调用之前,在test1(1,2)前边,一下两种都可以 def test1(x,y): print(x,y) logger('Test1') def logger(sou ...
- H1ctf-Vote
用来练习IO_FILE利用 glibc-2.23 # coding:utf-8 from pwn import * from FILE import * context.arch = 'amd64' ...
- C# 接口慨述
接口(interface)用来定义一种程序的协定.实现接口的类或者结构要与接口的定义严格一致.有了这个协定,就可以抛开编程语言的限制(理论上).接口可以从多个基接口继承,而类或结构可以实现多个接口.接 ...
- 图片url转base64
var xhr = new XMLHttpRequest() // 配置的代理,解决跨域问题 xhr.open('GET', url.replace('http://xxx.com', '/img') ...
- Win 无法安装 python 包
Win 上使用 pip install 安装出错 使用 wheel 安装 pip install wheel 下载 编译包 http://www.lfd.uci.edu/~gohlke/pythonl ...
- linux关于权限
用户权限:drwxr-x---. 8 root root 4096 8月 6 23:18 mnt 第一个root:所有者 即root用户第二个root:所有者所在的组mnt:所有者创建的文件夹Rwx: ...