每天一道算法题

This commit is contained in:
zeek 2020-03-23 19:52:48 +08:00
parent 863e298e91
commit a2d5676d0e

27
src/list/MiddleNode.java Normal file
View 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];
}
}