Prechádzať zdrojové kódy

add 从尾到头打印链表

tangs 2 rokov pred
rodič
commit
bf052b2d46

+ 25 - 0
leetcode/剑指offer/从尾到头打印链表/main.go

@@ -0,0 +1,25 @@
+package main
+
+func main() {
+
+}
+
+type ListNode struct {
+	Val  int
+	Next *ListNode
+}
+
+func reversePrint(head *ListNode) []int {
+	var tmpArr []int
+	for head != nil {
+		tmpArr = append(tmpArr, head.Val)
+		head = head.Next
+	}
+
+	var ret []int
+	for i := len(tmpArr) - 1; i >= 0; i-- {
+		ret = append(ret, tmpArr[i])
+	}
+
+	return ret
+}