日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

链表问题(6)-----排序

發(fā)布時(shí)間:2025/7/14 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 链表问题(6)-----排序 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、題目:將單向鏈表按某值劃分為左邊小、中間相等、右邊大的形式

簡單的思路:時(shí)間O(N),空間O(N)

采用一個(gè)數(shù)組來存儲鏈表中的值,然后對該數(shù)組進(jì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 #將鏈表存儲在數(shù)組中arr = []while head:arr.append(head.value)head = head.nextarr = partition(arr,pivot) #創(chuàng)建鏈表 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)

進(jìn)階思想:時(shí)間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)

?


二、題目:實(shí)現(xiàn)單鏈表的選擇排序:

要求:額外空間為O(1),時(shí)間復(fù)雜度為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)

?


三、題目:實(shí)現(xiàn)單鏈表的歸并排序

在?O(n?log?n) 時(shí)間復(fù)雜度和常數(shù)級空間復(fù)雜度下,對鏈表進(jìn)行排序。

思路:時(shí)間復(fù)雜度O(nlogn),空間復(fù)雜度O(1)

1、找到鏈表中間節(jié)點(diǎn)

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

?


四、對鏈表進(jìn)行插入排序

時(shí)間復(fù)雜度為O(n2),空間復(fù)雜度為O(1)

思路:

一個(gè)函數(shù)遍歷原鏈表到當(dāng)前元素,一個(gè)函數(shù)將當(dāng)前元素插入到已經(jīng)排好序的鏈表中,最終返回已經(jīng)排好序的列表。

代碼:

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

?

轉(zhuǎn)載于:https://www.cnblogs.com/Lee-yl/p/9747437.html

總結(jié)

以上是生活随笔為你收集整理的链表问题(6)-----排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。