题解 | #反转链表#

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @return ListNode类

#sys.setrecursionlimit(100000)
class Solution:
    def ReverseList(self , head: ListNode) -> ListNode:
        # write code here
        #if head is None or head.next is None:
            #return head
        '''
        newhead = self.ReverseList(head.next)
        head.next.next = head
        head.next = None
        return newhead
        '''

        '''
        if not head: return head
        res = []
        while head:
            res.append(head)
            head = head.next

        res = list(reversed(res))
        for i in range(len(res) - 1):
            res[i].next = res[i+1]

        res[-1].next = None
        #print(res[0])
        return res[0]

        '''

        temp = head
        res = []
        while temp is not None:
            res.append(temp.val)
            temp = temp.next
        res.reverse()

        temp = head
        for i in range(len(res)):
            temp.val = res[i]
            temp = temp.next

        return  head

#python方法就要用python的方法#
全部评论

相关推荐

10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:我了个雷 1.实习经历写太长了吧,精简一点,你写那么老多,面试官看着都烦 2.项目经历你放俩竞赛干啥单独拿出来写上几等奖就行了呗 3.一大雷点就是项目经历里的那个课程设计,大家都知道课程设计巨水,不要写课程设计,换一个名字,就叫学生管理系统,面试官问就说是自己做的项目,不要提课程设计的事 4.那个交流经历,简化一下塞到最上面的教育经历里就行了 5.简历尽量一页纸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务