python3的链表大礼包 单向链表实现及翻转链表

class LNode(object):
    def __init__(self,data):
        self.data=data
        self.next=None
    def set_next(self,next):
        self.next=next
    def get_data(self):
        return self.data
    def get_next(self):
        return self.next
class LinkList(object):
    def __init__(self):
        self.head=None
    def gethead(self):
        return self.head
    def add(self,item):
        temp=LNode(item)
        p=self.head
        if self.isEmpty():
            self.head=temp
        else:
            while p!=None:
                pre=p
                p=p.next
            pre.next=temp
    def initList(self,data):
        self.head=LNode(data[0])
        p=self.head
        for i in data[1:]:
            node=LNode(i)
            p.next=node
            p=p.next
        return self.head

    def isEmpty(self):
        if self.head==None:
            return 1
        else:
            return 0

    def getLength(self):
        if self.isEmpty():
            exit(0)
        p=self.head
        len=0
        while p!=None:
            len+=1
            p=p.next
        return len

    def traveList(self,head):
        if head==None:
            exit(0)
        out=[]
        p=head
        while p!=None:
            out.append(p.data)
            p=p.next
        print(out)
    def find(self,x):
        if self.isEmpty():
            exit(0)
        p=self.head
        num=0
        while p!=None:
            if p.data==x:
                return num
            else:
                p=p.next
                num+=1
        return -1

    def insertElem(self,key,index):
        if self.isEmpty():
            exit(0)
        if index<0 or index>self.getLength()-1:
            exit(0)
        p=self.head
        i=0
        while i<=index:
            pre=p
            p=p.next
            i+=1
        node=LNode(key)
        pre.next=node
        node.next=p


    def deleteElem(self,index):
        if self.isEmpty():
            exit(0)
        if index<0 or index>self.getLength()-1:
            exit(0)
        i=0
        p=self.head
        while p.next!=None:
            pre=p
            p=p.next
            i+=1
            if i==index:
                pre.next=p.next
                p=None
                return
        pre.next=None

    def reverse1(self):  # 将链表逆序函数
        res=[]
        if self.isEmpty():
            exit(0)
        p=self.head
        while p!=None:
            res.append(p.data)
            p=p.next
        while(len(res)!=0):
            print(res.pop(),end=' ')

    def reverse2(self,head):  # 循环的方法反转链表
        if head is None or head.next is None:
            return head
        # 定义反转的初始状态
        pre = None
        cur = head
        newhead = head
        while cur:
            newhead = cur
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        return newhead

    def reverse3(self,head,newhead):  # 递归,head为原链表的头结点,newhead为反转后链表的头结点
        if head is None:
            return
        if head.next is None:
            newhead = head
        else:
            newhead = self.reverse3(head.next, newhead)
            head.next.next = head #循环指
            head.next = None
        return newhead

def sortMy(head):
    if head==None or head.next==None:
        return head
    mid=getmid(head)
    l=head
    r=mid.next
    mid.next=None
    return merge(sortMy(l),sortMy(r))


def merge(p,q):
    temp=LNode(0)
    h=temp
    while p and q:
        if p.data<q.data:
            h.next=p
            p=p.next
        else:
            h.next=q
            q=q.next
        h=h.next
    if p:
        h.next=p
    if q:
        h.next=q
    return temp.next



def getmid(head):
    if head==None:
        return head
    fast=slow=head
    while fast.next and fast.next.next:
        slow=slow.next
        fast=fast.next.next
    return slow

data=[1,2,3,4,5]
S=LinkList()
S.initList(data)
S.add(1)
S.add(2)
f=S.gethead()
print("测试初始化和add函数")
S.traveList(f)
print("测试find函数")
print(S.find(5))
print("测试reverse1()函数,利用栈")
S.reverse1()
print()
print("测试reverse2()函数,利用三指针循环")
f=S.gethead()
head2=S.reverse2(f)
S.traveList(head2)
print("测试reverse3()函数,利用递归")
newhead=None
head3=S.reverse3(head2,newhead)
S.traveList(head3)



 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值