牛客-剑指0ffer-反转链表

题目描述
输入一个链表,反转链表后,输出新链表的表头。

# -*- 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
        pre = None
        middle = pHead
        after = None
        while(middle != None):
            after = middle.next
            middle.next = pre
            pre = middle
            middle = after
        return pre

你可能感兴趣的:(牛客-剑指0ffer-反转链表)