获取链表的中点
- 时间复杂度O(n),空间复杂度O(1)
- Runtime: 68 ms, faster than 89.71%
- Memory Usage: 39 MB, less than 19.40%
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var middleNode = function(head) {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
};
网友评论