-
876. Middle of the Linked List javaLeetCode_Study_Plan/Programming Skills 2022. 9. 24. 14:15
https://leetcode.com/problems/middle-of-the-linked-list/?envType=study-plan&id=programming-skills-i
/** * 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) { Map<Integer, ListNode> map = new HashMap(); ListNode curr_head = head; int cnt = 0; for (int i = 0; curr_head != null; i++) { map.put(cnt, curr_head); curr_head = curr_head.next; cnt++; } int middle = Math.round(cnt / 2); return map.get(middle); } }
cnt와 i를 적절하게 활용하면 메모리도 아낄 수 있었을텐데 지속적인 에러가 자꾸 떠서 일단은 둘 다 써서 해결해봤다.
'LeetCode_Study_Plan > Programming Skills' 카테고리의 다른 글
404. Sum of Left Leaves java (0) 2022.09.24 104. Maximum Depth of Binary Tree java (0) 2022.09.24 1290. Convert Binary Number in a Linked List to Integer java (0) 2022.09.24 953. Verifying an Alien Dictionary java (0) 2022.09.22 1309. Decrypt String from Alphabet to Integer Mapping java (0) 2022.09.22