cubegao

Swift.从尾到头打印链表

2015-07-01

题目描述:输入一个链表,从尾到头打印链表每个节点的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
import Foundation

class For05Solution {
func printListNode(_ head: ListNode?) {

if head?.next != nil {
printListNode(head?.next)
}

print(head!.val)
}
}

算法思想:采用递归的解法,利用栈的原理。

github地址:https://github.com/cubegao/LeetCode

Tags: 算法

扫描二维码,分享此文章