反转单向和双向链表
【題目】 分別實現反轉單向鏈表和反轉雙向鏈表的函數
【要求】 如果鏈表長度為N,時間復雜度要求為O(N),額外空間 復雜度要求為O(1)
def reverseList(head):if head == None:returnpre = Nonewhile head!=None:next_ = head.nexthead.next = prepre = headhead = next_return predef reverseDoubleList(head):if head == None:returnpre = Nonewhile head!=None:next_ = head.nexthead.next = prehead.pre = next_pre = headhead = next_return pre?
總結
- 上一篇: “之”字形打印矩阵
- 下一篇: 在行列都排好序的矩阵中找数