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 ...
随机推荐
- 报错:Program bash is not found in PATH
(如果按照我的方法来的话是没有这个错误的,我之前用别的方法的时候有但是后来还是没解决,写出来放到这里做参考吧) 参考原文:http://blog.csdn.net/fuyongbing1986/art ...
- python any all函数
a = [0, 0, 0, 0] b = [0, 0, 0, 1] c = [1, 1, 1, 1] >>> any(a) False >>> any(b) Tru ...
- Sqlserver计算本年度工作日
--@StartDate 本年度第一天 --@EndDate 本年度最后一天 , ) , DATEADD(yy, , , )) IF EXISTS ( SELECT * FROM tempdb..sy ...
- 设置RichTextBox控件的文本的对齐方式
实现效果: 知识运用: RichTextBox控件的SelectionAlignment属性 //获取或设置在当前选择或插入点的对齐方式 public HorizontalAlignment Sele ...
- PAT (Basic Level) Practise (中文)- 1004. 成绩排名 (20)
http://www.patest.cn/contests/pat-b-practise/1004 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入 ...
- JQuery从服务器端取得数据绑定到dropdownlist(select)中
http://blog.csdn.net/gaofang2009/article/details/5840783 http://www.cnblogs.com/Mac_Hui/archive/2010 ...
- Spring Cloud学习介绍
最近在学spring cloud, 整理了下 简单知识要求: 1.要了解springboot 2.了解分布式架构 3.了解微服务 4.了解springcloud是做什么的 带着这些,初学者 就至少有个 ...
- Nginx+proxy_cache图片缓存
搭建图片缓存机制的原理在于减少数据库的负担并加快静态资源的响应. 步骤: 1. vim /usr/local/nginx/conf/nginx.conf 2. http{ ... .. ...
- django 图片上传与显示
由于图片上传的需要,学习了一波上传 1. 上传 前端代码 <form action="写上相应的定向位置" method="post" enctype=& ...
- Linux-WebServer安装和配置
Apache 基本操作 解释 命令 安装 yum install httpd 启动 service httpd start 停止 service httpd stop 启动完成后 查看进程是否存在:p ...