Algorithm/LeetCode

    [LeetCode] - LFU Cache

    Design and implement a data structure for a Least Frequently Used (LFU) cache. Implement the LFUCache class: LFUCache(int capacity) Initializes the object with the capacity of the data structure. int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1. void put(int key, int value) Update the value of the key if present, or inserts the key if not already p..

    [LeetCode] - Linked List Cycle

    링크드 리스트이 반복적으로 순회하고 있다면 true 아니라면 false Constraints: The number of the nodes in the list is in the range [0, 10^4]. -105

    [LeetCode] - Palindrome Linked List

    Linked List가 주어지고 해당 리스트가 회문으로 구성되어 있으면 true 아니면 false 생각해보자 Palindrome이란 회문이다. 회문이란 패턴이 반복되는것을 의미한다. LinkedList에서 회문을 검증하기 위해서는 head가 null이 될 때 까지 순회하며 패턴이 반복되는지를 체크해야한다. head가 null이라면 true를 리턴 그렇지 않다면 패턴이 반복된다는 것을 컴퓨터 시점에서 생각해보자. ex) 1 1 2 2 1 head가 null이 아님 1 next 2 next 2 next 1 next null out 1 next 2 next null 회문이라면 중간에 같은 수가 반복되는 부분이 필요 그렇다면 temp같은 변수를 둬서 직전에 수와 같은 부분이 없다면 return false 홀수라..

    [LeetCode] - Merge Two Sorted Lists

    두 singly-Linked List를 오름차순으로 하나의 리스트로 합치기 Constraints: The number of nodes in both lists is in the range [0, 50]. -100 first) { mergeList.next = l2; l2 = l2.next; }else { mergeList.next = l1; mergeList.next = l2; l1 = l1.next; l2 = l2.next; } } return mergeList; } } Run Code Status: Runtime Error Your input [1,2,4] [1,3,4] java.lang.NullPointerException at line 34, Solution.mergeTwoLists 내일 계속 /..

    [LeetCode] - Remove Nth Node From End of List

    LinkedList의 head와 끝에서 n번째의 노드의 위치를 가지고 n번째의 노드를 지우고 list를 반환 1차 시도 /** * 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 removeNthFromEnd(ListNode head, int n) { int listSize = 0; // 순회하면서..

    [LeetCode] - Delete Node in a Linked List

    단일 연결 목록에서 노드를 삭제하는 함수를 작성하라. list의 헤드에 대한 액세스 권한이 부여되지 않고 대신 직접 삭제 될 노드에 대한 액세스 권한이 부여됩니다. 삭제할 노드는 list의 tail 노드가 아니다. Constraints: The number of the nodes in the given list is in the range [2, 1000]. -1000 9로 바뀌었다 그렇다면 val을 바꿔주면 어떨까 class Solution { public void deleteNode(ListNode node) { ListNode temp = null; node.val = node.next.val; temp = node.next.next; node.next = temp; } } Runtime: 0 ms..