add: 链表的中间节点
This commit is contained in:
25
src/list/middle-of-the-linked-list.js
Normal file
25
src/list/middle-of-the-linked-list.js
Normal file
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* function ListNode(val) {
|
||||
* this.val = val;
|
||||
* this.next = null;
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
* @param {ListNode} head
|
||||
* @return {ListNode}
|
||||
*/
|
||||
export const middleNode = function (head) {
|
||||
if (!head.next) return head
|
||||
if (head.next && !head.next.next) return head.next
|
||||
|
||||
let slow = head
|
||||
let fast = head.next.next
|
||||
|
||||
while (fast.next && fast.next.next) {
|
||||
slow = slow.next
|
||||
fast = fast.next.next
|
||||
}
|
||||
|
||||
return fast.next ? slow.next.next : slow.next
|
||||
}
|
Reference in New Issue
Block a user