题目描述
输入一个链表,反转链表后,输出新链表的表头。
解题思路
直接在原链表上操作,不需要新的链表
python solution:
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
pre,next = None,None
while pHead is not None:
next = pHead.next
pHead.next = pre
pre = pHead
pHead = next
return pre
原文地址:https://www.cnblogs.com/bernieloveslife/p/10423295.html
时间: 2024-11-13 09:44:08