NC 71 反转链表(python)

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=188&&tqId=36997&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking

题目描述

输入一个链表,反转链表后,输出新链表的表头。
示例1

输入  {1,2,3}

返回值  {3,2,1}

  

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        if not pHead:
            return None
        prev, cur = None, pHead
        while cur:
            cur.next, prev, cur = prev, cur, cur.next
        return prev

 

posted @ 2021-01-08 17:29  蒋励  阅读(83)  评论(0编辑  收藏  举报