1. 程式人生 > >反轉連結串列[劍指offer]之python實現

反轉連結串列[劍指offer]之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

遞迴的呼叫