From 835bb5e3fd62668507931680b9a4d592e2f0444c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=C2=B7=E5=B0=8F=E6=9D=8E?= <875402161@qq.com> Date: Wed, 31 Aug 2022 15:12:51 +0000 Subject: [PATCH] =?UTF-8?q?update=20lcof/=E9=9D=A2=E8=AF=95=E9=A2=9806.=20?= =?UTF-8?q?=E4=BB=8E=E5=B0=BE=E5=88=B0=E5=A4=B4=E6=89=93=E5=8D=B0=E9=93=BE?= =?UTF-8?q?=E8=A1=A8/README.md.=20=E6=96=B0=E5=A2=9E=E9=80=92=E5=BD=92?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 程序员·小李 <875402161@qq.com> --- .../README.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" index 144b7f9a39..a4a0a07091 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" @@ -110,6 +110,42 @@ class Solution { } ``` +递归实现: + +```java +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public int[] reversePrint(ListNode head) { + List values = new ArrayList(); + printNode(head, values); + + int[] result = new int[values.size()]; + int index = 0; + for (Integer ele : values){ + result[index++] = ele; + } + return result; + } + + private void printNode(ListNode head, List values){ + if (head != null){ + Integer val = head.val; + if (head.next != null){ + printNode(head.next, values); + } + values.add(val); + } + } +} +``` + ### **JavaScript** ```js -- Gitee