lintcode練習 98 鍊錶排序

2021-08-23 12:28:15 字數 4004 閱讀 5549

在 o(n log n) 時間複雜度和常數級的空間複雜度下給鍊錶排序。

給出1->3->2->null,給它排序變成1->2->3->null.

分別用歸併排序和快速排序做一遍。

實現**:

"""

definition of listnode

class listnode(object):

def __init__(self, val, next=none):

self.val = val

self.next = next

"""'''

#歸併排序

class solution:

"""@param head: the head of linked list.

@return: you should return the head of the sorted linked list, using constant space complexity.

"""def sortlist(self, head):

# write your code here

if head is none or head.next is none:

return head

#找到鍊錶的中點結點

mid = self.findmid(head)

#先執行鍊錶中點往後

right = self.sortlist(mid.next)

#將右半部分拋棄,則得到左半部分

mid.next = none

left = self.sortlist(head)

return self.mergetwolists(left, right)

def findmid(self, head):

slow, fast = head, head.next

#slow移動一位, fast移動兩位,當fast遍歷完,slow正好處於中點

while fast != none and fast.next != none:

fast = fast.next.next

slow = slow.next

return slow

def mergetwolists(self, l1, l2):

# write your code here

dummy = listnode(0)

tmp = dummy

while l1 != none and l2 != none:

if l1.val < l2.val:

tmp.next = l1

l1 = l1.next

else:

tmp.next = l2

l2 = l2.next

tmp = tmp.next

if l1 != none:

tmp.next = l1

elif l2 != none:

tmp.next = l2

return dummy.next

''''''

#快速排序

class solution:

"""@param head: the head of linked list.

@return: you should return the head of the sorted linked list, using constant space complexity.

"""def sortlist(self, head):

# write your code here

if head is none or head.next is none:

return head

mid = self.findmid(head)

leftdummy, rightdummy, middummy = listnode(0),listnode(0),listnode(0)

lefttail, righttail, midtail = leftdummy, rightdummy, middummy

while head:

#如果該結點的值小於中間結點的值,就加入左鍊錶

if head.val < mid.val:

lefttail.next = head

lefttail = lefttail.next

#如果該結點的值大於中間結點的值,就加入右鍊錶

elif head.val > mid.val:

righttail.next = head

righttail = head

#如果該結點的值等於於中間結點的值,就加入中間鍊錶

else:

midtail.next = head

midtail = head

head = head.next

#鍊錶的尾部指向none

lefttail.next, righttail.next, midtail.next = none, none, none

#對左右鍊錶開始遞迴

left = self.sortlist(leftdummy.next)

right = self.sortlist(rightdummy.next)

#合併三個鍊錶

return self.concat(left, middummy.next, right)

#找到鍊錶的中點結點

def findmid(self, head):

slow, fast = head, head.next

#slow移動一位, fast移動兩位,當fast遍歷完,slow正好處於中點

while fast != none and fast.next != none:

fast = fast.next.next

slow = slow.next

return slow

#合併三個鍊錶

def concat(self, left, mid, right):

dummy = listnode(0)

tail = dummy

tail.next = left

#找到合併後鍊錶的尾結點

tail = self.gettail(tail)

tail.next = mid

tail = self.gettail(tail)

tail.next = right

return dummy.next

#找到鍊錶的尾結點

def gettail(self, head):

if head is none:

return none

while head.next:

head = head.next

return head

'''#利用內建函式排序

class solution:

"""@param head: the head of linked list.

@return: you should return the head of the sorted linked list, using constant space complexity.

"""def sortlist(self, head):

# write your code here

from functools import cmp_to_key

nodes =

while head:

head = head.next

dummy = listnode(0)

node = dummy

for n in sorted(nodes, key=cmp_to_key(self.nodesort)):

node.next = n

node = node.next

return dummy.next

def nodesort(self, a, b):

return a.val - b.val

lintcode 98 鍊錶排序

在 o n log n 時間複雜度和常數級的空間複雜度下給鍊錶排序。樣例給出 1 3 2 null,給它排序變成 1 2 3 null.挑戰分別用歸併排序和快速排序做一遍。標籤鍊錶 思路 採用歸併排序 時間複雜度是o nlogn 的排序有快速排序 歸併排序 堆排序 使用快慢指標找出鍊錶中點。code...

領扣LintCode演算法問題答案 98 鍊錶排序

領扣lintcode演算法問題答案 98.鍊錶排序 題解鳴謝 在 o n log n 時間複雜度和常數級的空間複雜度下給鍊錶排序。輸入 1 3 2 null 輸出 1 2 3 null輸入 1 7 2 6 null 輸出 1 2 6 7 null definition for listnode pu...

lintcode練習 102 帶環鍊錶

給定乙個鍊錶,判斷它是否有環。給出 21 10 4 5,tail connects to node index 1,返回 true 不要使用額外的空間 實現 思路 快慢指標的典型應用,使用快指標 fast 與慢指標 slow,slow每次後移一位,fast 每次後移兩位,當fast 與 slow 指...