题解 | #反转链表#

反转链表

https://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
        # 设置一个链表用于返回接收
        res = None
        while pHead != None:
            # 需要一个结点暂时存放pHead的下一个结点,不然会指针丢失
            temp = pHead.next
            # 原来的头结点指向返回结果的结点
            pHead.next = res
            # 都往后挪一位,进行下次循环
            res = pHead
            pHead = temp
        return res

#我的实习求职记录#
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-11 13:34
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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