Question
876. Middle of the Linked List
Solution
题目大意:求链表的中间节点
思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完链表,另一个恰好在中间
Java实现:
public ListNode middleNode(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null) { fast = fast.next; if(fast != null) { fast = fast.next; slow = slow.next; } } return slow; }
来源:https://www.cnblogs.com/okokabcd/p/9406821.html