欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

剑指offer-15.反转链表-Python

程序员文章站 2022-06-17 17:46:46
...

反转链表

题目描述

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

记录

三个指针合作:
剑指offer-15.反转链表-Python
举例:
剑指offer-15.反转链表-Python

# -*- 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 is None or pHead.next is None:
            return pHead
        pre = None
        while pHead:
        	#tmp暂存下一个节点
            tmp = pHead.next
            pHead.next = pre
            pre = pHead
            pHead = tmp
        return pre