-
876. Middle of the Linked List javaLeetCode_Study_Plan/Algorithm 2022. 12. 21. 00:06
https://leetcode.com/problems/middle-of-the-linked-list/
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode middleNode(ListNode head) { ListNode temp = head; int n = 0; for (; temp != null; temp = temp.next) { n++; } for (int i = 0; head != null; head = head.next) { if (i == Math.round(n/2)) { return head; } i++; } return head; } }
'LeetCode_Study_Plan > Algorithm' 카테고리의 다른 글
567. Permutation in String java (0) 2022.12.21 🎵 19. Remove Nth Node From End of List java (0) 2022.12.21 🎵 557. Reverse Words in a String III java (0) 2022.12.20 344. Reverse String java (0) 2022.12.20 167. Two Sum II - Input Array Is Sorted java (0) 2022.12.20