File tree Expand file tree Collapse file tree 4 files changed +115
-1
lines changed Expand file tree Collapse file tree 4 files changed +115
-1
lines changed Original file line number Diff line number Diff line change 38
38
│ ├── README.md
39
39
│ ├── Solution.java
40
40
│ └── Solution.py
41
- └── 面试题11. 旋转数组的最小数字
41
+ ├── 面试题11. 旋转数组的最小数字
42
+ │ ├── README.md
43
+ │ ├── Solution.java
44
+ │ └── Solution.py
45
+ └── 面试题22. 链表中倒数第k个节点
42
46
├── README.md
43
47
├── Solution.java
44
48
└── Solution.py
Original file line number Diff line number Diff line change
1
+ # [ 面试题22. 链表中倒数第k个节点] ( https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/ )
2
+
3
+ ## 题目描述
4
+ 输入一个链表,输出该链表中倒数第 k 个节点。为了符合大多数人的习惯,本题从 1 开始计数,即链表的尾节点是倒数第 1 个节点。例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6。这个链表的倒数第 3 个节点是值为 4 的节点。
5
+
6
+
7
+ ** 示例:**
8
+
9
+ 给定一个链表: 1->2->3->4->5, 和 k = 2.
10
+
11
+ 返回链表 4->5.
12
+
13
+ ## 解法
14
+ ### Python3
15
+ ``` python
16
+ # Definition for singly-linked list.
17
+ # class ListNode:
18
+ # def __init__(self, x):
19
+ # self.val = x
20
+ # self.next = None
21
+
22
+ class Solution :
23
+ def getKthFromEnd (self , head : ListNode, k : int ) -> ListNode:
24
+ if not (head or head.next):
25
+ return head
26
+
27
+ p = q = head
28
+ for _ in range (k):
29
+ p = p.next
30
+ while p:
31
+ p = p.next
32
+ q = q.next
33
+ return q
34
+
35
+ ```
36
+
37
+ ### Java
38
+ ``` java
39
+ /**
40
+ * Definition for singly-linked list.
41
+ * public class ListNode {
42
+ * int val;
43
+ * ListNode next;
44
+ * ListNode(int x) { val = x; }
45
+ * }
46
+ */
47
+ class Solution {
48
+ public ListNode getKthFromEnd (ListNode head , int k ) {
49
+ if (head == null || head. next == null ) {
50
+ return head;
51
+ }
52
+ ListNode p = head, q = head;
53
+ while (k-- > 0 ) {
54
+ p = p. next;
55
+ }
56
+ while (p != null ) {
57
+ p = p. next;
58
+ q = q. next;
59
+ }
60
+ return q;
61
+ }
62
+ }
63
+ ```
64
+
65
+ ### ...
66
+ ```
67
+
68
+ ```
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 ListNode getKthFromEnd (ListNode head , int k ) {
11
+ if (head == null || head .next == null ) {
12
+ return head ;
13
+ }
14
+ ListNode p = head , q = head ;
15
+ while (k -- > 0 ) {
16
+ p = p .next ;
17
+ }
18
+ while (p != null ) {
19
+ p = p .next ;
20
+ q = q .next ;
21
+ }
22
+ return q ;
23
+ }
24
+ }
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 getKthFromEnd (self , head : ListNode , k : int ) -> ListNode :
9
+ if not (head or head .next ):
10
+ return head
11
+
12
+ p = q = head
13
+ for _ in range (k ):
14
+ p = p .next
15
+ while p :
16
+ p = p .next
17
+ q = q .next
18
+ return q
You can’t perform that action at this time.
0 commit comments