【剑指offer】16. 反转链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014568072/article/details/87480576

题目描述

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

思路

《剑指offer》P112

code

# -*- 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 not pHead:
            return 
        pPre = None
        pCurr = pHead
        pNext = pHead.next
        while pNext:
            pCurr.next = pPre
            pPre = pCurr
            pCurr = pNext
            pNext = pNext.next
        pCurr.next = pPre
        return pCurr

猜你喜欢

转载自blog.csdn.net/u014568072/article/details/87480576