【剑指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
        head = ListNode(-1)
        while pHead is not None:
            tmp = head.next
            head.next = pHead
            pHead = pHead.next
            head.next.next = tmp
         
        return head.next
发布了99 篇原创文章 · 获赞 6 · 访问量 4009

猜你喜欢

转载自blog.csdn.net/weixin_42247922/article/details/103910789