美文网首页LeetCode刷题记录
LeetCode 876. 链表的中间结点

LeetCode 876. 链表的中间结点

作者: TheKey_ | 来源:发表于2019-06-26 21:23 被阅读2次

    876. 链表的中间结点

    给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
    如果有两个中间结点,则返回第二个中间结点。

    示例 1:

    输入:[1,2,3,4,5]
    输出:此列表中的结点 3 (序列化形式:[3,4,5])
    返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
    注意,我们返回了一个 ListNode 类型的对象 ans,这样:
    ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.

    示例 2:

    输入:[1,2,3,4,5,6]
    输出:此列表中的结点 4 (序列化形式:[4,5,6])
    由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。

    提示:

    给定链表的结点数介于 1 和 100 之间。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/middle-of-the-linked-list
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


    • 1.常规解法

    思路:
    1.遍历该链表,计算该链表的大小 size
    2.找出最中间的位置 mid = size / 2
    3.找到 mid 索引所对应的节点, 返回该节点即可

    public static class ListNode {
    
            private int val;
            private ListNode next;
    
            public ListNode(int val) {
                this.val = val;
            }
    
            //用于测试用例
            public ListNode(int[] arr) {
                if (arr == null || arr.length == 0) throw new NullPointerException("array is Empty");
                this.val = arr[0];
                ListNode cur = this;
                for (int i = 1; i < arr.length; i++) {
                    cur.next = new ListNode(arr[i]);
                    cur = cur.next;
                }
            }
    
            @Override
            public String toString() {
                StringBuilder res = new StringBuilder();
                ListNode cur = this;
                while (cur != null) {
                    res.append(cur.val + "->");
                    cur = cur.next;
                }
                res.append("NULL");
                return res.toString();
            }
    
        }
    
        public static ListNode middleNode(ListNode head) {
            if (head == null || head.next == null) return head;
            int size = 0;
            ListNode cur = head;
            while (cur != null) {
                size++;
                cur = cur.next;
            }
            int mid = size / 2;
            ListNode p = head;
            for (int i = 0; i < mid; i++) {
                p = p.next;
            }
            return p;
        }
    

    复杂度分析:
    时间复杂度:O(n):n是创建的节点数目
    空间复杂度:O(1)

    • 2.快慢指针法

    思路:(图解来源于leetcode)
    1.新建两个指针 fast、slow,让fast指针的移动速度是slow的2倍
    2.那么当链表长度为奇数时,当fast到最后,slow正好在中间,即为我们需要的
    3.当链表长度为偶数时,fast正好指向最后一个节点的下一个节点,而slow只需在向后移动一个位置即可

    image.png
    public static ListNode middleNode(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode slow = head;
            ListNode fast = head;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
    

    复杂度分析:
    时间复杂度:O(n),其中 n 是给定列表的结点数目。
    空间复杂度:O(1),slow 和 fast 用去的空间。

    • 3.数组法

    思路:
    1.创建一个ListNode数组,将链表中的元素依次添加到数组中
    2.取出数组中最中间的元素即可

    public static ListNode middleNode(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode[] arr = new ListNode[100];
            int index = 0;
            ListNode cur = head;
            while (cur != null) {
                arr[index++] = cur;
                cur = cur.next;
            }
            return arr[index / 2];
        }
    

    复杂度分析:
    时间复杂度:O(n),其中 n 是给定列表的结点数目。
    空间复杂度:O(n), n为数组用去的空间

    • 测试用例

    public static void main(String[] args) {
            int[] arr = new int[] {1, 2, 3, 4, 5};
            ListNode listNode = new ListNode(arr);
            System.out.println(listNode);
            System.out.println("链表的中间节点为:" + middleNode(listNode));
        }
    
    • 结果

    1->2->3->4->5->NULL
    链表的中间节点为:3->4->5->NULL
    

    • 源码

    • 我会随时更新新的算法,并尽可能尝试不同解法,如果发现问题请指正
    • Github

    相关文章

      网友评论

        本文标题:LeetCode 876. 链表的中间结点

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