链表python笔试题目_python经典面试算法题1.4:如何对链表进行重新排序
本題目摘自《Python程序員面試算法寶典》,我會每天做一道這本書上的題目,并分享出來,統(tǒng)一放在我博客內(nèi),收集在一個分類中。
1.4 對鏈表按照如下要求重新排序
【微軟筆試題】
難度系數(shù):???
考察頻率:????
題目描述:
給定鏈表L0 -> L1 -> L2 -> … -> Ln-1 -> Ln ,把鏈表重新排序為 L0 -> Ln -> L1 -> Ln-1 -> L2 … 。要求:(1)在原來鏈表的基礎上進行排序,即不能申請新的結點;(2)只能修改結點的next域,不能修改數(shù)據(jù)域。
分析解答:
當我們在筆試過程中遇到這種問題,一下子是沒有什么思路的,我們要做的方法是把復雜問題拆解為簡單問題。
就這一題而言我們可以把它分為三個步驟,(1)首先找到鏈表的中間結點;(2)對鏈表的后半部分子鏈表進行逆序;(3)把鏈表的前半部分子鏈表與逆序后的后半部分子鏈表進行合并,這里合并的思路是分別從兩個鏈表各取一個結點進行合并。
步驟一
我們使用兩個指針從鏈表的第一個結點開始同時遍歷,快指針每次走兩步,慢指針每次走一步,當快指針到達鏈表尾部時,慢指針恰好到達鏈表中部。
def find_mid_node(head):
if head is None or head.next is None:
return head
fast = head
slow = head
prev = slow
while fast is not None and fast.next is not None:
fast = fast.next.next
prev = slow
slow = slow.next
prev.next = None
return slow # 下半段的head
步驟二
這一步在我前面的博客文章中有寫,可以點擊此處跳轉(zhuǎn)。
def reverse(head): #
p = head
if p is None or p.next is None:
return p
q = p
p = p.next
while p is not None:
tmp = p.next
p.next = q
if q == head:
q.next = None
q = p
p = tmp
return q
步驟三
這個函數(shù)傳入前半部分的第一個結點和后半部分的第一個結點,cur1指向前半部分的第一個結點,cur2指向后半部分的第一個結點,tmp指向cur1后面的結點,然后令cur1的next指向cur2,接著cur1后移指向此時的tmp,然后讓tmp再指向記住cur2的后面一個結點,令cur2的next指向此時的cur1,這樣我們就完成了后半部分的第一個結點插入到前半部分,就這樣一直往后,直到cur1到達最后一個結點,全部結點插入完成。
# 插入
def reorder(head1, head2):
cur1 = head1
cur2 = head2
tmp = None
while cur1.next is not None:
tmp = cur1.next
cur1.next = cur2
cur1 = tmp
tmp = cur2.next
cur2.next = cur1
cur2 = tmp
cur1.next = cur2 # 無論cur2是不是None,cur的next都應該指向cur2
把三個函數(shù)封裝到一個類中
我們定義一個類,把上面三個函數(shù)都放在類中,并且令類的實例變成可調(diào)用對象。
class ReorderLink:
@staticmethod
def find_mid_node(head):
if head is None or head.next is None:
return head
fast = head
slow = head
prev = slow
while fast is not None and fast.next is not None:
fast = fast.next.next
prev = slow
slow = slow.next
prev.next = None
return slow # 下半段的head
@staticmethod
def reverse(head): #
p = head
if p is None or p.next is None:
return p
q = p
p = p.next
while p is not None:
tmp = p.next
p.next = q
if q == head:
q.next = None
q = p
p = tmp
return q
@staticmethod
def reorder(head1, head2):
cur1 = head1
cur2 = head2
tmp = None
while cur1.next is not None:
tmp = cur1.next
cur1.next = cur2
cur1 = tmp
tmp = cur2.next
cur2.next = cur1
cur2 = tmp
cur1.next = cur2 # 無論cur2是不是None,cur的next都應該指向cur2
def __call__(self, link_head):
before = link_head
rest =self.find_mid_node(before)
rest = self.reverse(rest) # 反轉(zhuǎn)
self.reorder(before, rest)
return link_head
測試
構造一個鏈表,創(chuàng)建一個類的實例,調(diào)用類的實例。
class Node: # 結點類
def __init__(self, data=None):
self.data = data
self.next = None
class LinkList: # 鏈表
def __init__(self):
self.head = None
def append(self, x):
if self.head is None:
self.head = Node(x)
return self
p = self.head
while p.next is not None:
p = p.next
p.next = Node(x)
return self
link1 = LinkList()
link1.append(1).append(2).append(3).append(4).append(5).append(6)
head9 = link1.head
p = head9
print("排序前: ", end=" ")
while p is not None:
print(p.data, end="\t")
p = p.next
instance = ReorderLink() # 實例
head9 = instance(head9) # 實例可調(diào)用
print()
p = head9
print("排序后: ", end=" ")
while p is not None:
print(p.data, end="\t")
p = p.next
最后:
if hasattr(reader, "QQ"):
print(f"請{reader}加入交流群:6259 88679 !")
總結
以上是生活随笔為你收集整理的链表python笔试题目_python经典面试算法题1.4:如何对链表进行重新排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3 匹配空格 正则_玩转正则
- 下一篇: python竖排文本_Calibre 5