876:链表的中间结点
This commit is contained in:
parent
acef8b361c
commit
31978afb9a
73
src/main/java/leetcode/editor/cn/MiddleOfTheLinkedList.java
Normal file
73
src/main/java/leetcode/editor/cn/MiddleOfTheLinkedList.java
Normal file
@ -0,0 +1,73 @@
|
||||
//给定一个头结点为 head 的非空单链表,返回链表的中间结点。
|
||||
//
|
||||
// 如果有两个中间结点,则返回第二个中间结点。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:[1,2,3,4,5]
|
||||
//输出:此列表中的结点 3 (序列化形式:[3,4,5])
|
||||
//返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
|
||||
//注意,我们返回了一个 ListNode 类型的对象 ans,这样:
|
||||
//ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next =
|
||||
//NULL.
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:[1,2,3,4,5,6]
|
||||
//输出:此列表中的结点 4 (序列化形式:[4,5,6])
|
||||
//由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 给定链表的结点数介于 1 和 100 之间。
|
||||
//
|
||||
// Related Topics 链表
|
||||
// 👍 349 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//876:链表的中间结点
|
||||
public class MiddleOfTheLinkedList{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new MiddleOfTheLinkedList().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode middleNode(ListNode head) {
|
||||
List<ListNode> list = new ArrayList<>();
|
||||
while (head != null) {
|
||||
list.add(head);
|
||||
head = head.next;
|
||||
}
|
||||
return list.get(list.size() / 2);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
32
src/main/java/leetcode/editor/cn/MiddleOfTheLinkedList.md
Normal file
32
src/main/java/leetcode/editor/cn/MiddleOfTheLinkedList.md
Normal file
@ -0,0 +1,32 @@
|
||||
<p>给定一个头结点为 <code>head</code> 的非空单链表,返回链表的中间结点。</p>
|
||||
|
||||
<p>如果有两个中间结点,则返回第二个中间结点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>[1,2,3,4,5]
|
||||
<strong>输出:</strong>此列表中的结点 3 (序列化形式:[3,4,5])
|
||||
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
|
||||
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
|
||||
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>[1,2,3,4,5,6]
|
||||
<strong>输出:</strong>此列表中的结点 4 (序列化形式:[4,5,6])
|
||||
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给定链表的结点数介于 <code>1</code> 和 <code>100</code> 之间。</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 349</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user