链表问题(6)-----排序
生活随笔
收集整理的這篇文章主要介紹了
链表问题(6)-----排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、題目:將單向鏈表按某值劃分為左邊小、中間相等、右邊大的形式
簡單的思路:時間O(N),空間O(N)
采用一個數組來存儲鏈表中的值,然后對該數組進行快排,然后再連成鏈表,快排思想如下圖所示:
代碼:
class Node:def __init__(self,value):self.value = valueself.next = None #快排思想 def partition(arr,pivot):small = -1big = len(arr)index = 0while index != big:if arr[index] < pivot:small += 1arr[small] , arr[index] = arr[index] , arr[small]index += 1elif arr[index] == pivot:index += 1else:big -= 1arr[index] , arr[big] = arr[big] , arr[index]return arr #鏈表操作 def listSort(head,pivot):if not head:return head #將鏈表存儲在數組中arr = []while head:arr.append(head.value)head = head.nextarr = partition(arr,pivot) #創建鏈表 node = Node(arr[0])p = nodefor i in range(1,len(arr)):p.next = Node(arr[i])p = p.nextreturn nodehead = Node(9) head.next = Node(0) head.next.next = Node(4) head.next.next.next = Node(3) head.next.next.next.next = Node(1) pivot = 3 node = listSort(head,pivot)進階思想:時間O(N),空間O(1)
?
代碼:
class Node:def __init__(self,value):self.value = valueself.next = None def listPartition(head,pivot):if not head:return headsH = NonesT = NoneeH = NoneeT = NonebH = NonebT = None while head:next = head.nexthead.next = Noneif head.value < pivot:if not sH:sH = headsT = headelse:sT.next = headsT = headelif head.value == pivot:if not eH:eH = headeT = headelse:eT.next = headeT = headelse:if bH == None:bH = headbT = headelse:bT.next = headbT = headhead = nextif sT:sT.next = eHeT = eT if eT else sTif eT:eT.next = bHreturn sH if sH else eH if eH else bHarr = [9,1,4,5,6,3,8] head = Node(arr[0]) p = head for i in range(1,len(arr)):p.next = Node(arr[i])p = p.next pivot = 5 res = listPartition(head,pivot)?
二、題目:實現單鏈表的選擇排序:
要求:額外空間為O(1),時間復雜度為O(N2)
代碼:
?
class Node:def __init__(self,val):self.val = valself.next = None def sortList(head):if not head:return headtail = Noneres = tailcur = headsmallpre = Node(None)small = Node(None)while cur:small = cursmallPre = getSmallValue(cur)if smallPre: small = smallPre.next smallPre.next = small.nextcur = cur.next if cur == small else curif not tail:tail = smallres = smallelse:tail.next = smalltail = small def getSmallValue(cur):small = cursmallPre = Nonewhile cur.next:if cur.next.val < small.val:small = cur.nextsmallPre = curcur = cur.nextreturn smallPre head = Node(4) head.next = Node(5) head.next.next = Node(3) head.next.next.next = Node(1) head.next.next.next.next = Node(2) sortList(head)?
三、題目:實現單鏈表的歸并排序
在?O(n?log?n) 時間復雜度和常數級空間復雜度下,對鏈表進行排序。
思路:時間復雜度O(nlogn),空間復雜度O(1)
1、找到鏈表中間節點
2、遞歸排序左邊鏈表和右邊鏈表。
3、排序合并左右兩邊鏈表
class ListNode(object):def __init__(self, x):self.val = xself.next = Noneclass Solution(object):def sortList(self, head):""":type head: ListNode:rtype: ListNode"""if not head or not head.next:return headmid = self.get_mid(head)l = headr = mid.nextmid.next = Nonereturn self.merge(self.sortList(l),self.sortList(r))def get_mid(self,head):if not head or not head.next:return Noneslow = headfast = headwhile fast.next and fast.next.next:slow = slow.nextfast = fast.next.nextreturn slowdef merge(self,l,r):head = ListNode(0)tmp = headwhile l and r:if l.val <= r.val:tmp.next = ll = l.nextelse:tmp.next = rr = r.nexttmp = tmp.nextif l:tmp.next = lif r:tmp.next = rreturn head.next?
四、對鏈表進行插入排序
時間復雜度為O(n2),空間復雜度為O(1)
思路:
一個函數遍歷原鏈表到當前元素,一個函數將當前元素插入到已經排好序的鏈表中,最終返回已經排好序的列表。
代碼:
lass ListNode(object): # def __init__(self, x): # self.val = x # self.next = Noneclass Solution(object):def insertionSortList(self, head):""":type head: ListNode:rtype: ListNode"""if not head or not head.next:return headsortnode = Nonecur = headwhile cur:node = curcur = cur.nextnode.next = Nonesortnode = self.sortList(sortnode,node)return sortnodedef sortList(self,head,node):pre = Nonecur = headwhile cur and node.val > cur.val:pre = curcur = cur.nextnode.next = curif cur == head:head = nodeelse:pre.next = nodereturn head?
轉載于:https://www.cnblogs.com/Lee-yl/p/9747437.html
總結
以上是生活随笔為你收集整理的链表问题(6)-----排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快速理解编码,unicode与utf-8
- 下一篇: Console控制台的正确打开方式