python编程练习:三种方法实现链表的逆序

#链表的存储特点:可以用任意一组存储单元来存储单链表中的数据元素,而且除了存储每个数据元素外,还必须存储
#指示其直接后继元素的信息
#实现链表的逆序
#方法1:就地逆序
#在遍历链表的时候,修改当前结点指针域的指向,让其指向它的前驱结点。需要用一个指针变量来保存前驱结点的地址,
#此外为了在调整当前结点指针域的指向后还能找到后继结点还需要另外一个指针变量来保存后继结点的地址,在所有结点
#都被保存好以后就可以直接完成指针的逆序,还需要注意链表首尾结点的特殊处理
class LNode(object):
    def __init__(self,x=None):
        self.data = x
        self.next = None
#这里需要注意的是需要为x赋值None,以及函数名为__init__
#否则可能出现的报错
#TypeError: __new__() missing 1 required positional argument: 'x'
#AttributeError: 'NoneType' object has no attribute 'next'
def Reverse(head):
    #判断链表是否为空
    if head == None or head.next == None or head.next.next == None:
        return
    pre = None#前驱结点
    cur = None#当前结点
    next = None#后继结点
    #把链表首结点变成尾结点
    cur = head.next
    next = cur.next
    cur.next = None
    pre = cur
    cur = next
    #使当前遍历到的结点指向前结点
    while cur.next != None:
        next = cur.next
        cur.next = pre
        pre = cur
        cur = cur.next
        cur = next
    #链表最后一个结点指向倒数第二个结点
    cur.next = pre
    #链表的头节点指向原来链表的尾结点
    head.next = cur

#方法2:递归,对不带头结点的单链表进行逆序
#假定原链表为1->2->3->4->5->6->7,递归法的主要思路为:先逆序除第一个结点以外的子链表
#将1->2->3->4->5->6->7变为1->7->6->5->4->3->2
#接着把结点1添加到逆序的子链表的后面,1->7->6->5->4->3->2变为7->6->5->4->3->2->1
#同理在逆序链表2->3->4->5->6->7时,也是先逆序子链表3->4->5->6->7
def RecursiveReverse(head):
    #如果链表为空或者链表中只有一个元素
    if head is None or head.next is None:
        return head
    else:
        #反转后面的结点
        newhead = RecursiveReverse(head.next)
        head.next.next=head
        head.next = None
    return newhead
#对带头结点的单链表进行逆序
def Reverse2(head):
    if head is None:
        return
    #获取链表第一个结点
    firstNode = head.next
    #对链表进行逆序
    newhead = RecursiveReverse(firstNode)
    #头结点指向逆序后链表的第一个结点
    head.next = newhead
    return newhead

#方法3:插入法,从链表的第二个结点开始,把遍历到的结点插入到头结点的后面,知道遍历结束
#假定原链表为head->1->2->3->4->5->6->7,在遍历到2的时候,将其插入到头结点后,链表变为head->2->1->3->4->5->6->7
#同理将后续遍历到的所有结点都插入到头结点head后,就可以实现链表的逆序

def Reverse3(head):
    #判断链表是否为空
    if head is None or head.next is None:
        return
    cur = None#当前结点
    next = None#后继结点
    cur = head.next.next
    #设置链表第一个结点为尾结点
    head.next.next = None
    #把遍历到结点插入到头结点的后面
    while cur is not None:
        next = cur.next
        cur.next = head.next
        head.next = cur
        cur = next
if __name__ == "__main__":
    i = 1
    #链表头结点
    head = LNode()
    head.next = None
    tmp = None
    cur = head
    #构造单链表
    while i < 8:
        tmp = LNode()
        tmp.data = i
        tmp.next = None
        cur.next = tmp
        cur = tmp
        i += 1
    print("逆序前:")
    cur = head.next
    while cur != None:
        print(cur.data)
        cur = cur.next
    print("\n逆序后:")
    Reverse(head)
    cur = head.next
    while cur != None:
        print(cur.data)
        cur = cur.next
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值