反转链表(important!)

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

解题思路
直接在原链表上操作,不需要新的链表
pre->pHead->next

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
posted @ 2019-03-02 17:53  bernieloveslife  阅读(78)  评论(0编辑  收藏  举报