美文网首页
876. Middle of the Linked List

876. Middle of the Linked List

作者: jluemmmm | 来源:发表于2021-11-30 11:53 被阅读0次

获取链表的中点

  • 时间复杂度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;
};

相关文章

网友评论

      本文标题:876. Middle of the Linked List

      本文链接:https://www.haomeiwen.com/subject/yvplxrtx.html