题解 | #反转链表#

反转链表

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

# -*- coding:utf-8 -*-
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
            
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        if pHead == None or pHead.next == None:
            return pHead
        else:
            nodes = [pHead]
            tmp=pHead.next
            while tmp: 
                nodes.append(tmp)
                tmp = tmp.next
            nodes.reverse()
            for idx, each in enumerate(nodes[:-1]):
                each.next = nodes[idx+1]
            nodes[-1].next = None
            return nodes[0]
全部评论

相关推荐

06-04 17:59
已编辑
长江大学 Java
点赞 评论 收藏
分享
嵌入式求职之路:可以看我经验😂,https://www.nowcoder.com/share/jump/73221730841876945
点赞 评论 收藏
分享
06-04 16:50
腾讯_TEG_技术
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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