一、前言
本系列文章為《劍指Offer》刷題筆記,
刷題平臺:牛客網
二、題目
輸入一個鏈表,回傳一個反序的鏈表,
1、思路
可以直接使用串列的插入手法,每次插入資料,只插入在首位,
2、編程實作
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : Ma Yi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2020-04-22 # Name : test03 # Software : PyCharm # Note : 輸入一個鏈表,回傳一個反序的鏈表, # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 回傳從尾部到頭部的串列值序列,例如[1,2,3] def print_list_from_tail_to_head(self, list_node): # write code here result = [] while list_node: result.insert(0, list_node.val) list_node = list_node.next return result
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/155302.html
標籤:Python
上一篇:劍指Offer-002:替換空格
