题解 | #反转链表#

反转链表

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

python3解法 借助reversed函数反转列表或者列表自身特性直接反转

# -*- 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
        head = ListNode(0)
        p = head
        tmp_list = []
        while pHead is not None:
            tmp_list.append(pHead.val)
            pHead = pHead.next
        # for item in tmp_list[::-1]:
        for item in reversed(tmp_list):
            p.next = ListNode(item)
            p = p.next
        return head.next
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
码农索隆:想看offer细节
点赞 评论 收藏
分享
昨天 13:37
重庆大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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