码迷,mamicode.com
首页 > 其他好文 > 详细

876. Middle of the Linked List - LeetCode

时间:2018-08-02 15:05:17      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:遍历   分享图片   linked   node   src   ems   etc   节点   ext   

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;
}

876. Middle of the Linked List - LeetCode

标签:遍历   分享图片   linked   node   src   ems   etc   节点   ext   

原文地址:https://www.cnblogs.com/okokabcd/p/9406821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!