Skip to content

Commit 9ccc261

Browse files
链表排序
1 parent ece99c9 commit 9ccc261

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

每日一题/147.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'''
2+
Descripttion: 对链表进行插入排序
3+
version: 1
4+
Author: Jason
5+
Date: 2020-11-20 10:03:56
6+
LastEditors: Jason
7+
LastEditTime: 2020-11-21 07:40:47
8+
'''
9+
# Definition for singly-linked list.
10+
import random
11+
12+
13+
def GenerateRandomList(number, size):
14+
temp = list()
15+
random_legth = random.randint(0, size)
16+
current_length = 0
17+
while current_length < random_legth:
18+
temp.append(random.randint(1, number))
19+
current_length += 1
20+
return temp
21+
22+
23+
class ListNode:
24+
def __init__(self, x):
25+
self.val = x
26+
self.next = None
27+
28+
29+
class Solution:
30+
def insertionSortList(self, head: ListNode) -> ListNode:
31+
32+
if not head:
33+
return None
34+
dummy = ListNode(0)
35+
dummy.next = head
36+
cur = head
37+
while cur and cur.next:
38+
if cur.val <= cur.next.val:
39+
cur = cur.next
40+
41+
else:
42+
temp = cur.next
43+
cur.next = cur.next.next
44+
pre = dummy
45+
46+
while temp.val >= pre.next.val:
47+
pre = pre.next
48+
temp.next = pre.next
49+
pre.next = temp
50+
51+
return dummy.next
52+
53+
def insertionSortList2(self, head: ListNode) -> ListNode:
54+
ans = ListNode(float("-inf"))
55+
56+
while head:
57+
next = head.next
58+
cur = ans
59+
while cur.next and cur.next.val < head.val:
60+
cur = cur.next
61+
head.next = cur.next
62+
cur.next = head
63+
head = next
64+
return ans.next
65+
66+
67+
if __name__ == "__main__":
68+
s = Solution()
69+
for _ in range(100):
70+
nums = GenerateRandomList(20, 10)
71+
if len(nums) < 1:
72+
continue
73+
nodes = [ListNode(each_num) for each_num in nums]
74+
sorted_nums = list(sorted(nums))
75+
# 连接链表
76+
for i in range(len(nodes) - 1):
77+
nodes[i].next = nodes[i + 1]
78+
res = s.insertionSortList2(nodes[0])
79+
insertsort_li = list()
80+
cur = res
81+
while cur:
82+
insertsort_li.append(cur.val)
83+
cur = cur.next
84+
if insertsort_li != sorted_nums:
85+
print("wrong:", nums)
86+
print("Done")
87+
'''
88+
89+
'''

每日一题/148.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'''
2+
Descripttion: 排序链表
3+
version: 1
4+
Author: Jason
5+
Date: 2020-11-21 08:10:16
6+
LastEditors: Jason
7+
LastEditTime: 2020-11-21 11:48:00
8+
'''
9+
import random
10+
11+
12+
def GenerateRandomList(number, size):
13+
temp = list()
14+
random_legth = random.randint(0, size)
15+
current_length = 0
16+
while current_length < random_legth:
17+
temp.append(random.randint(1, number))
18+
current_length += 1
19+
return temp
20+
21+
22+
class ListNode:
23+
def __init__(self, x):
24+
self.val = x
25+
self.next = None
26+
27+
28+
class Solution:
29+
def sortList(self, head: ListNode) -> ListNode:
30+
if not head or not head.next:
31+
return head
32+
33+
mid = self.getMid(head)
34+
right_head = mid.next
35+
mid.next = None
36+
37+
left = self.sortList(head)
38+
right = self.sortList(right_head)
39+
40+
return self.merge(left, right)
41+
42+
def getMid(self, head: ListNode) -> ListNode:
43+
# 找到中点-slow
44+
slow = head
45+
fast = head
46+
# 通过以下while循环,找到链表的中点-slow
47+
while fast.next:
48+
if fast.next:
49+
fast = fast.next
50+
else:
51+
break
52+
if fast.next:
53+
fast = fast.next
54+
else:
55+
break
56+
slow = slow.next
57+
return slow
58+
59+
def merge(self, left_head: ListNode, right_head: ListNode) -> ListNode:
60+
left_cur = left_head
61+
right_cur = right_head
62+
dummy_head = ListNode(float('-inf'))
63+
cur = dummy_head
64+
while left_cur and right_cur:
65+
if left_cur.val < right_cur.val:
66+
cur.next = left_cur
67+
left_cur = left_cur.next
68+
else:
69+
cur.next = right_cur
70+
right_cur = right_cur.next
71+
cur = cur.next
72+
cur.next = left_cur if left_cur else right_cur
73+
return dummy_head.next
74+
75+
76+
if __name__ == "__main__":
77+
s = Solution()
78+
for _ in range(100):
79+
nums = GenerateRandomList(20, 10)
80+
if len(nums) < 1:
81+
continue
82+
nodes = [ListNode(each_num) for each_num in nums]
83+
sorted_nums = list(sorted(nums))
84+
# 连接链表
85+
for i in range(len(nodes) - 1):
86+
nodes[i].next = nodes[i + 1]
87+
res = s.sortList(nodes[0])
88+
insertsort_li = list()
89+
cur = res
90+
while cur:
91+
insertsort_li.append(cur.val)
92+
cur = cur.next
93+
if insertsort_li != sorted_nums:
94+
print("wrong:", nums)
95+
print("Done")
96+
# li1 = [3, 1, 12, 6, 1, 9, 19, 4, 12]
97+
# li2 = [2, 4, 6, 8, 10]
98+
# nodes1 = [ListNode(each_num) for each_num in li1]
99+
# nodes2 = [ListNode(each_num) for each_num in li2]
100+
# # 连接链表
101+
# for i in range(len(nodes1) - 1):
102+
# nodes1[i].next = nodes1[i + 1]
103+
# for j in range(len(nodes2) - 1):
104+
# nodes2[j].next = nodes2[j + 1]
105+
# insertsort_li = list()
106+
# res = s.sortList(nodes1[0])
107+
# cur = res
108+
# while cur:
109+
# insertsort_li.append(cur.val)
110+
# cur = cur.next
111+
# if insertsort_li != sorted(li1):
112+
# print("wrong:")

0 commit comments

Comments
 (0)