File tree 5 files changed +113
-2
lines changed
5 files changed +113
-2
lines changed Original file line number Diff line number Diff line change 11
11
│ ├── README.md
12
12
│ ├── Solution.java
13
13
│ └── Solution.py
14
- └── 面试题 01.02. 判定是否互为字符重排
14
+ ├── 面试题 01.02. 判定是否互为字符重排
15
+ │ ├── README.md
16
+ │ └── Solution.py
17
+ └── 面试题 01.03. URL化
15
18
├── README.md
16
19
└── Solution.py
17
20
```
Original file line number Diff line number Diff line change 7
7
.
8
8
├── README.md
9
9
├── template.md
10
- └── 面试题03. 数组中重复的数字
10
+ ├── 面试题03. 数组中重复的数字
11
+ │ ├── README.md
12
+ │ └── Solution.py
13
+ ├── 面试题04. 二维数组中的查找
14
+ │ ├── README.md
15
+ │ ├── Solution.java
16
+ │ └── Solution.py
17
+ └── 面试题05. 替换空格
11
18
├── README.md
19
+ ├── Solution.java
12
20
└── Solution.py
13
21
```
14
22
Original file line number Diff line number Diff line change
1
+ # [ 面试题06. 从尾到头打印链表] ( https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/ )
2
+
3
+ ## 题目描述
4
+ 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
5
+
6
+ ** 示例 1:**
7
+ ```
8
+ 输入:head = [1,3,2]
9
+ 输出:[2,3,1]
10
+ ```
11
+
12
+ ** 限制:**
13
+
14
+ - ` 0 <= 链表长度 <= 10000 `
15
+
16
+ ## 解法
17
+ ### Python3
18
+ ``` python
19
+ # Definition for singly-linked list.
20
+ # class ListNode:
21
+ # def __init__(self, x):
22
+ # self.val = x
23
+ # self.next = None
24
+
25
+ class Solution :
26
+ def reversePrint (self , head : ListNode) -> List[int ]:
27
+ res = []
28
+ while head:
29
+ res.append(head.val)
30
+ head = head.next
31
+ return res[::- 1 ]
32
+ ```
33
+
34
+ ### Java
35
+ ``` java
36
+ /**
37
+ * Definition for singly-linked list.
38
+ * public class ListNode {
39
+ * int val;
40
+ * ListNode next;
41
+ * ListNode(int x) { val = x; }
42
+ * }
43
+ */
44
+ class Solution {
45
+ public int [] reversePrint (ListNode head ) {
46
+ Stack<Integer > s = new Stack<> ();
47
+ while (head != null ) {
48
+ s. push(head. val);
49
+ head = head. next;
50
+ }
51
+ int [] res = new int [s. size()];
52
+ int i = 0 ;
53
+ while (! s. isEmpty()) {
54
+ res[i++ ] = s. pop();
55
+ }
56
+ return res;
57
+ }
58
+ }
59
+ ```
60
+
61
+ ### ...
62
+ ```
63
+
64
+ ```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) { val = x; }
7
+ * }
8
+ */
9
+ class Solution {
10
+ public int [] reversePrint (ListNode head ) {
11
+ Stack <Integer > s = new Stack <>();
12
+ while (head != null ) {
13
+ s .push (head .val );
14
+ head = head .next ;
15
+ }
16
+ int [] res = new int [s .size ()];
17
+ int i = 0 ;
18
+ while (!s .isEmpty ()) {
19
+ res [i ++] = s .pop ();
20
+ }
21
+ return res ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode:
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution :
8
+ def reversePrint (self , head : ListNode ) -> List [int ]:
9
+ res = []
10
+ while head :
11
+ res .append (head .val )
12
+ head = head .next
13
+ return res [::- 1 ]
You can’t perform that action at this time.
0 commit comments