LeetCode No.143 | StriveZs的博客

LeetCode No.143

LeetCode第143题—重排链表

即将回家!

自己代码的开源仓库:click here 欢迎Star和Fork :)

题目描述

给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

1
2
3
4
5
6
示例 1:

给定链表 1->2->3->4, 重新排列为 1->4->2->3.
示例 2:

给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.

代码

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import copy
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution(object):
def reorderList(self, head):
"""
分析题目可以得到这个链表就是通过中间节点划分之后,前端链表和后端链表合并的结果
因此可以先用快慢指针来找到中间节点,然后用栈存储后端链表节点
最后实现前端链表和栈节点合并即可
:type head: ListNode
:rtype: None Do not return anything, modify head in-place instead.
"""
if head is None or head.next is None:
return head
fast = head # 快指针
slow = head # 慢指针
mid = None # 中间节点
forward = [] # 用于存储前向链表的队列
stack = [] # 用于存储后端链表的栈
# 快慢指针寻找中间节点
while fast is not None and fast.next is not None:
fast = fast.next.next
forward.append(slow)
slow = slow.next
mid = slow
# 遍历后端链表节点,将所有节点都放到栈里面
node = mid
while node is not None:
stack.append(node)
node = node.next
# 合并前端链表和反向后端链表
node = head
temp = stack[-1]
stack.pop()
node.next = temp
node = node.next
for i in range(1,len(forward)):
node.next = forward[i]
node = node.next
temp = stack[-1]
stack.pop()
node.next = temp
node = node.next
if len(stack) != 0:
node.next = stack[-1]
node = node.next
node.next = None

return head

if __name__ == '__main__':
root = ListNode(1)
node1 = ListNode(2)
node2 = ListNode(3)
node3 = ListNode(4)

root.next = node1
node1.next = node2
node2.next = node3

s = Solution()
t = s.reorderList(root)
while t is not None:
print(t.val)
t = t.next
StriveZs wechat
Hobby lead  creation, technology change world.