leetcode(21)
-
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 -
Remove Nth Node From End of List
SolutionGiven the head of a linked list, remove the nth node from the end of the list and return its head. ExamplesExample 1:Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5] Example 2:Input: head = [1], n = 1Output: [] Example 3:Input: head = [1,2], n = 1Output: [1] Constraints:The number of nodes in the list is sz.1 0 1 Explanationclass Solution { public ListNode removeNthFromEnd(ListNode h..
2024.11.06 -
String to Integer (atoi)
SolutionImplement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.The algorithm for myAtoi(string s) is as follows: Whitespace: Ignore any leading whitespace (" ").Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity is neither present.Conversion: Read the integer by skipping leading zeros until a non-digit characte..
2024.10.22