每天一道算法题
This commit is contained in:
parent
863e298e91
commit
a2d5676d0e
27
src/list/MiddleNode.java
Normal file
27
src/list/MiddleNode.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package list;
|
||||||
|
|
||||||
|
class ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode next;
|
||||||
|
public ListNode(int x) {
|
||||||
|
val = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
|
||||||
|
* 如果有两个中间结点,则返回第二个中间结点。
|
||||||
|
* https://leetcode-cn.com/problems/middle-of-the-linked-list/
|
||||||
|
*/
|
||||||
|
public class MiddleNode{
|
||||||
|
|
||||||
|
public ListNode middleNode(ListNode head) {
|
||||||
|
ListNode[] arr = new ListNode[100];
|
||||||
|
int size = 0;
|
||||||
|
while(head != null){
|
||||||
|
arr[size++] = head;
|
||||||
|
head = head.next;
|
||||||
|
}
|
||||||
|
return arr[size / 2];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user