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 this in-place without altering the nodes’ values.

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

分析

分四步:

  1. 遍历链表获得长度
  2. 找到链表中点,断开成两个链表
  3. 翻转第二个链表
  4. 将两个链表合并

时间复杂度O(n),空间O(1)

代码

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

class (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

l = 0
cur = head
while cur:
l += 1
cur = cur.next
m = (l - 1) / 2

cur = head
for i in xrange(m):
cur = cur.next

head2 = cur.next
cur.next = None

# now we have two lists, reverse the second one
prev = head2
cur = head2.next
prev.next = None
while cur:
next = cur.next
cur.next = prev
prev = cur
cur = next
head2 = prev

# then we merge the two lists
while head2:
n1 = head.next
n2 = head2.next
head.next = head2
head2.next = n1
head = n1
head2 = n2