码迷,mamicode.com
首页 > 编程语言 > 详细

剑指offer python版 反转链表

时间:2018-10-25 18:04:53      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:反转   init   print   pre   code   link   one   color   last   

class ListNode(object):
    def __init__(self,x):
        self.val=x
        self.next=None
        
class Link(object):
    def __init__(self,values=None):
        self.nodes=self.set_link(values) if  values else None
        
    def get_link(self):
        return self.nodes
    def set_link(self,values):
        if not values :
            return False
        head=ListNode(0)
        move=head
        
        
        try:
            for i in values:
                tmp=ListNode(i)
                move.next=tmp
                move=move.next
                
        except Exception as e:
            print(e)
            
        return head.next
def aa(head):
    if not head and not head.next:
        return head
    then=head.next
    head.next=None
    last=then.next
    while then:
        then.next=head
        head=then
        then=last
        if then:
            last=then.next
            
    return head.val

a=Link([1,2,3,3,4,5,6,7])

b=a.get_link()

print (aa(b))

 

剑指offer python版 反转链表

标签:反转   init   print   pre   code   link   one   color   last   

原文地址:https://www.cnblogs.com/xzm123/p/9851171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!