Skip to content

Commit dffb0d8

Browse files
committed
feat: add java/python solution for lcof problem
1 parent a8f2289 commit dffb0d8

File tree

5 files changed

+113
-2
lines changed

5 files changed

+113
-2
lines changed

lcci/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
│   ├── README.md
1212
│   ├── Solution.java
1313
│   └── Solution.py
14-
└── 面试题 01.02. 判定是否互为字符重排
14+
├── 面试题 01.02. 判定是否互为字符重排
15+
│   ├── README.md
16+
│   └── Solution.py
17+
└── 面试题 01.03. URL化
1518
├── README.md
1619
└── Solution.py
1720
```

lcof/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
.
88
├── README.md
99
├── 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. 替换空格
1118
├── README.md
19+
├── Solution.java
1220
└── Solution.py
1321
```
1422

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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]

0 commit comments

Comments
 (0)