1. 程式人生 > >python 反轉連結串列 --遞迴

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 not pHead or not pHead.next:
            return pHead
        else:
            newHead=self.ReverseList(pHead.next)
            pHead.next.next=pHead
            pHead.next=None
            
            return newHead
,輸出新連結串列的表頭。