【LeetCode with Python】 Reorder List
2014-07-06 15:24
417 查看
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/reorder-list/
题目类型:
难度评价:★
本文地址:/article/1377501.html
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 this in-place without altering the nodes' values.
For example,
Given
原题页面:https://oj.leetcode.com/problems/reorder-list/
题目类型:
难度评价:★
本文地址:/article/1377501.html
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 this in-place without altering the nodes' values.
For example,
Given
{1,2,3,4}, reorder it to
{1,4,2,3}.
class Solution: # @param head, a ListNode # @return nothing def reorderList(self, head): if None == head: return cur = head count = 0 while None != cur: cur = cur.next count += 1 if count <= 2: return half_count = (count - 1) / 2 cur2 = head for i in range(0, half_count): cur2= cur2.next cur2_next = cur2.next cur2.next = None cur2 = reverse_list(cur2_next, None, None) cur1 = head time_count = int(count / 2) for i in range(0, time_count): cur2_next = cur2.next cur2.next = cur1.next cur1.next = cur2 cur1 = cur2.next cur2 = cur2_next
相关文章推荐
- 【leetcode】Reorder List (python)
- [leetcode]Reorder List @ Python
- [LeetCode]题解(python):143-Reorder List
- 【LEETCODE】143- Reorder List [Python]
- [Leetcode][python]Reorder List
- 【leetcode】Reorder List (python)
- leetcode 【 Reorder List 】python 实现
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- LeetCode:Reorder List
- 【LeetCode with Python】 Linked List Cycle
- LeetCode:Reorder List C++与Java实现
- leetcode Reorder List
- LeetCode Reorder List O(n) space空间解法
- LeetCode Reorder List
- [LeetCode] Reorder List, Solution
- LeetCode:Reorder List
- Reorder List [LeetCode]
- LeetCode-Reorder List
- leetcode之Reorder List