0
0

More than 1 year has passed since last update.

Leetcode 876. Middle of the Linked List

Last updated at Posted at 2022-12-05

876. Middle of the Linked List

難易度

Easy

アプローチ

Brute Force

package com.leetcode.easy;

public class MiddleOfTheLinkedList876 {
    public static void main(String[] args) {}

    public ListNode876 middleNode(ListNode876 head) {
        ListNode876 temp = head;

        int length = 0;
        while (temp != null) {
            temp = temp.next;
            length++;
        }

        length /= 2;
        temp = head;

        while (0 < length) {
            temp = temp.next;
            length--;
        }
        return temp;
    }
}

class ListNode876 {
    int val;
    ListNode876 next;

    ListNode876() {}

    ListNode876(int val) {
        this.val = val;
    }

    ListNode876(int val, ListNode876 next) {
        this.val = val;
        this.next = next;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0