92. Reverse Linked List II
2015-12-19 19:33
399 查看
class Solution(object): def reverseBetween(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ # 创建一个头,如果从第一个翻转,这样会很方便 origin = ListNode(0) origin.next = head head = origin # A|BCDF|RG : 如果这么截取,那么A是0.0,B是0.1,F是1.0,R是1.1 i = 1 while i < m: head = head.next i += 1 place0_0 = head place0_1 = head.next while i < n + 1: head = head.next i += 1 place1_0 = head place1_1 = head.next # 翻转后拼接头尾 self.reverse(place0_1, n - m) place0_0.next = place1_0 place0_1.next = place1_1 # 删除一开始加入的头 return origin.next def reverse(self, head, count): """ 翻转count个节点 :param head: 翻转开始点 :param count: 要翻转的节点的个数 """ if head is None or head.next is None or count == 0: return head count -= 1 self.reverse(head.next, count) head.next.next = head head.next = None
相关文章推荐
- celery 入门
- mysql字符集调整总结
- 【数据结构】选择排序
- UIButton
- js函数中使用el表达式传入多个参数时的问题
- Python基本数据类型之int 、 float
- 几个可能会用到的前端库
- “舞客“搭建小记
- 【数据结构】冒泡排序
- Matlab借助匿名函数求隐函数
- mac-os MySQL_python-1.2.4b4
- Windows程序设计--鼠标
- mysql简单操作一
- centos误删除文件如何恢复
- hashSet TreeMap 区别解析
- 第十四周项目3----验证二叉排序树相关算法
- Scroll View 控件以Thumbnail的方式显示一个目录的全部图片,相似图片浏览器
- Fix Various Update Errors In Ubuntu 14.04
- ZigBee串口发送(非协议栈)
- Google搜索语法