zoukankan      html  css  js  c++  java
  • 用python介绍4种常用的单链表翻转的方法

    这里给出了4种4种常用的单链表翻转的方法,分别是:

    开辟辅助数组,新建表头反转,就地反转,递归反转

    # -*- coding: utf-8 -*-
    '''
    链表逆序
    '''
    class ListNode:  
        def __init__(self,x):  
            self.val=x
            self.next=None 
     
    '''
    第一种方法:
    对于一个长度为n的单链表head,用一个大小为n的数组arr储存从单链表从头
    到尾遍历的所有元素,在从arr尾到头读取元素简历一个新的单链表
    时间消耗O(n),空间消耗O(n)
    '''       
    def reverse_linkedlist1(head):
        if head == None or head.next == None: #边界条件
            return head
        arr = [] # 空间消耗为n,n为单链表的长度
        while head:
            arr.append(head.val)
            head = head.next
        newhead = ListNode(0)
        tmp = newhead
        for i in arr[::-1]:
            tmp.next = ListNode(i)
            tmp = tmp.next
        return newhead.next
     
    '''
    开始以单链表的第一个元素为循环变量cur,并设置2个辅助变量tmp,保存数据;
    newhead,新的翻转链表的表头。
    时间消耗O(n),空间消耗O(1)
    '''
     
    def reverse_linkedlist2(head):
        if head == None or head.next == None: #边界条件
            return head
        cur = head #循环变量
        tmp = None #保存数据的临时变量
        newhead = None #新的翻转单链表的表头
        while cur:
            tmp = cur.next 
            cur.next = newhead
            newhead = cur   # 更新 新链表的表头
            cur = tmp
        return newhead
        
    '''
    开始以单链表的第二个元素为循环变量,用2个变量循环向后操作,并设置1个辅助变量tmp,保存数据;
    时间消耗O(n),空间消耗O(1)
    '''
     
     
    def reverse_linkedlist3(head):
        if head == None or head.next == None: #边界条件
            return head
        p1 = head #循环变量1
        p2 = head.next #循环变量2
        tmp = None #保存数据的临时变量
        while p2:
            tmp = p2.next
            p2.next = p1
            p1 = p2
            p2 = tmp
        head.next = None
        return p1
     
    '''
    递归操作,先将从第一个点开始翻转转换从下一个节点开始翻转
    直至只剩一个节点
    时间消耗O(n),空间消耗O(1)
    '''
     
    def reverse_linkedlist4(head):
        if head is None or head.next is None:
            return head
        else:
            newhead=reverse_linkedlist4(head.next)
            head.next.next=head
            head.next=None
        return newhead
     
            
    def create_ll(arr):
        pre = ListNode(0)
        tmp = pre
        for i in arr:
            tmp.next = ListNode(i)
            tmp = tmp.next
        return pre.next
        
    def print_ll(head):
        tmp = head
        while tmp:
            print tmp.val
            tmp=tmp.next
     
    a = create_ll(range(5))
    print_ll(a) # 0 1 2 3 4
    a = reverse_linkedlist1(a)
    print_ll(a) # 4 3 2 1 0
    a = reverse_linkedlist2(a)
    print_ll(a) # 0 1 2 3 4
    a = reverse_linkedlist3(a)
    print_ll(a) # 4 3 2 1 0
    a = reverse_linkedlist4(a)
    print_ll(a) # 0 1 2 3 4
    

    本文转载自:https://blog.csdn.net/u011452172/article/details/78127836 

  • 相关阅读:
    php对象 构造方法的使用
    php面向对象 类常量的介绍
    php面向对象 $this的使用
    php 对象里面的属性 ||增删改查
    php实现图片上传完整版
    php 下载文件的例子
    php 学生信息表小项目 扩展模仿百度分页的完全实现
    前后端分离 解读
    java Class.forName() 方法详解
    spring MVC 注解详解
  • 原文地址:https://www.cnblogs.com/petrolero/p/9822008.html
Copyright © 2011-2022 走看看