leetcode143Reorder List
来源:互联网 发布:与该网络签署协议 编辑:程序博客网 时间:2024/06/03 18:52
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reorderList(self, head):
"""
:type head: ListNode
:rtype: void Do not return anything, modify head in-place instead.
"""
if not head or not head.next:
return
mid=self.getmid(head)
r=self.reverse(mid.next)
mid.next=None
self.merge(head,r)
def getmid(self,head):
slow=head
fast=head.next
while fast and fast.next:
slow=slow.next
fast=fast.next.next
return slow
def reverse(self,head):
prev=None
while head:
tmp=head.next
head.next=prev
prev=head
head=tmp
return prev
def merge(self,l,r):
dummy=cur=ListNode(0)
while l and r:
cur.next=l
cur=l
l=l.next
cur.next=r
cur=r
r=r.next
if l:
cur.next=l
if r:
cur.next=r
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reorderList(self, head):
"""
:type head: ListNode
:rtype: void Do not return anything, modify head in-place instead.
"""
if not head or not head.next:
return
mid=self.getmid(head)
r=self.reverse(mid.next)
mid.next=None
self.merge(head,r)
def getmid(self,head):
slow=head
fast=head.next
while fast and fast.next:
slow=slow.next
fast=fast.next.next
return slow
def reverse(self,head):
prev=None
while head:
tmp=head.next
head.next=prev
prev=head
head=tmp
return prev
def merge(self,l,r):
dummy=cur=ListNode(0)
while l and r:
cur.next=l
cur=l
l=l.next
cur.next=r
cur=r
r=r.next
if l:
cur.next=l
if r:
cur.next=r
0 0
- leetcode143Reorder List
- List
- list
- List
- LIST
- list
- list
- List
- list
- list
- List
- list
- LIST
- List
- List
- List
- list
- list
- Vue.js——60分钟组件快速入门(下篇)
- javascript篇二
- Jdbc调用Sqlserver各版本
- 动脑学院高级ui效果之水波纹
- 【算法】程序猿不写代码是不对的50
- leetcode143Reorder List
- 并发和并行的区别
- C#--01--串口通信
- 你不可不知的 React Native 混合用法(Android 篇)
- linux中inittab文件详解
- [HTML] 图片标签、绝对路径 和 相对路径
- Android实现intent跳转界面传递数据(2)——onActivityResult方法接收返回值
- 刚体之间挤压造成的抖动问题
- 14 list与vector