Java(30)
-
Linked List Cycle
SolutionGiven head, the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.Return true if there is a..
2024.11.11 -
Palindrome Linked List
SolutionGiven the head of a singly linked list, return true if it is a palindrome or false otherwise. ExamplesExample 1:Input: head = [1,2,2,1]Output: true Example 2:Input: head = [1,2]Output: false Constraints:The number of nodes in the list is in the range [1, 105].0 Explanationclass Solution { public boolean isPalindrome(ListNode head) { // Find the middle ListNode ListNode sl..
2024.11.11 -
Merge Two Sorted Lists
SolutionYou are given the heads of two sorted linked lists list1 and list2.Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.Return the head of the merged linked list. ExamplesExample 1:Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4] Example 2:Input: list1 = [], list2 = []Output: [] Example 3:Input: list1 = [], li..
2024.11.08 -
Reverse Linked List
SolutionGiven the head of a singly linked list, reverse the list, and return the reversed list. ExamplesExample 1:Input: head = [1,2,3,4,5]Output: [5,4,3,2,1] Example 2:Input: head = [1,2]Output: [2,1] Example 3:Input: head = []Output: [] Explanationclass Solution { public ListNode reverseList(ListNode head) { ListNode newNode = null; while(head != null){ ListNode nex..
2024.11.07 -
Java Concurrency - Synchronized
The most typical one is access to shared data. In a multithreaded environment, conflicts occur when two or more threads try to update shared mutable data simultaneously. To solve this problem, a mechanism is provided. Logical fragments marked as "synchronized" become synchronization blocks, which ensures that at the same moment, only one thread can execute a certain method or a certain code bloc..
2024.11.03 -
Java Concurrency - DeadLock, LiveLock, Starvation
Deadlock It occurs when two threads are waiting for each other's resources, but at the same time, neither thread releases the resources it already holds, resulting in eternal blocking. public class DeadLock { static Object lock1 = new Object(); static Object lock2 = new Object(); public static void main(String[] args) { new Thread(()->{ try{ synchronize..
2024.11.02