题目描述

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

时间限制:1秒;空间限制:32768K;本题知识点:链表

解题思路

# -*- 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
        pre = None
        cur = pHead
        while cur:
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        return pre

 

相关文章:

  • 2022-12-23
  • 2021-06-03
  • 2021-08-13
  • 2021-05-04
  • 2021-09-12
  • 2022-01-10
  • 2021-09-17
  • 2021-09-04
猜你喜欢
  • 2018-04-07
  • 2021-10-25
  • 2021-12-18
  • 2021-08-19
  • 2022-01-10
  • 2022-12-23
相关资源
相似解决方案