题目:

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

代码: oj 测试通过 248 ms

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6.  
  7. class Solution:
  8. # @param head, a ListNode
  9. # @return nothing
  10. def reorderList(self, head):
  11. if head is None or head.next is None or head.next.next is None:
  12. return head
  13.  
  14. dummyhead = ListNode(0)
  15. dummyhead.next = head
  16.  
  17. # get the length of the linked list
  18. p = head
  19. list_length = 0
  20. while p is not None:
  21. list_length += 1
  22. p = p.next
  23.  
  24. #reverse the second half linked list
  25. fast = dummyhead
  26. for i in range((list_length+1)/2):
  27. fast = fast.next
  28. pre = fast
  29. curr = pre.next
  30. for i in range( (list_length)/2 - 1 ):
  31. tmp = curr.next
  32. curr.next = tmp.next
  33. tmp.next = pre.next
  34. pre.next = tmp
  35.  
  36. #merge
  37. h2 = pre.next
  38. fast.next = None # cut the connection between 1st half linked list and 2nd half linked list
  39. while head is not None and h2 is not None:
  40. tmp = head.next
  41. head.next = h2
  42. tmp2 = h2.next
  43. head.next.next = tmp
  44. h2 = tmp2
  45. head = tmp
  46.  
  47. return dummyhead.next

思路

这道题的路子分三块:

1. 遍历单链表 求链表长度

2. 锁定后半个链表,反转后半个链表的每个元素

3. 切断前后半个链表的链接处 然后合并两个链表

leetcode 【 Reorder List 】python 实现的更多相关文章

  1. [leetcode]Reorder List @ Python

    原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...

  2. 【leetcode】Reorder List (python)

    问题的思路是这样: 循环取头部合并,事实上也能够换个角度来看,就是将后面的链表结点,一次隔空插入到第一部分的链表中. class Solution: # @param head, a ListNode ...

  3. [LeetCode] Reorder List 链表重排序

    Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...

  4. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  5. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  6. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  7. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  8. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  9. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

  10. [LeetCode]题解(python):113 Path Sum II

    题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...

随机推荐

  1. Retrofit2与RxJava用法大全

    Retrofit2是square公司出品的一个网络请求库,网上有很多相关的介绍.我很久以前都想去研究了,但一直都有各种事情耽搁,现在就让我们一起去捋一捋,这篇主要讲解Retrofit2与RxJava的 ...

  2. HttpURLConnection(http 1.1) 用法、状态码、状态描述

    最近研究了java的HttpURLConnection的用法, 这里简单的做一下记录: Java中可以使用HttpURLConnection来请求WEB资源. 1. URL请求的类别 分为二类,GET ...

  3. Qt安装教程

    一.Qt下载 官网下载链接http://download.qt.io/archive/qt/,下载最新版 5.10 官网的下载网站有的时候可能会抽风,也可以选择国内的镜像下载源http://mirro ...

  4. sublime相关小技巧

    1.快速建立一个新文件:Ctrl+n 2.修改多个相同符号:Ctrl+D 3.建立语言后缀的文件保存,例如我想创建PHP的语言脚本,先按Ctrl+Shift+p,打开Command Palette,输 ...

  5. HDU5152 线段树 + 数论

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5152 ,线段树区间更新 + 点更新 + 数论知识(数论是重点QAQ),好题值得一做. BestCode ...

  6. Web前端学习流程

  7. javaweb基础(31)_国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...

  8. 文件系统inodes使用率过高问题处理

    运维过程中经常碰见文件系统inodes使用率过高导致文件系统不可写的问题,常见场景如下 .Oracle产生的审计文件,特别是DG备库或者审计设置为OS时 .crontab产生大量邮件,导致/var/s ...

  9. react的ant design的UI组件库

    PC官网:https://ant.design/ 移动端网址:https://mobile.ant.design/docs/react/introduce-cn antd-mobile :是 Ant ...

  10. 1269: [AHOI2006]文本编辑器editor

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5269  Solved: 2037[Submit][Status][Discuss] Descript ...