codingtest(12)
-
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 -
Implement strStr()
SolutionGiven two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. ExamplesExample 1:Input: haystack = "sadbutsad", needle = "sad"Output: 0Explanation: "sad" occurs at index 0 and 6.The first occurrence is at index 0, so we return 0. Example 2:Input: haystack = "leetcode", needle = "leeto"Output: -1Explanation: ..
2024.10.14 -
Valid Palindrome
SolutionA phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.Given a string s, return true if it is a palindrome, or false otherwise. Examples Explanationclass Solution { public boolean isPalindrome(String s) { S..
2024.10.11 -
Valid Anagram
SolutionGiven two strings s and t, return true if t is an anagram of s, and false otherwise. Examples Constraints:- 1 - s and t consist of lowercase English letters. Explanationclass Solution { public boolean isAnagram(String s, String t) { char[] sArr = s.toCharArray(); char[] tArr = t.toCharArray(); Arrays.sort(sArr); Arrays.sort(tArr); return Arra..
2024.10.10 -
First Unique Character in a String
SolutionGiven a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. ExamplesExample 1:Input: s = "leetcode"Output: 0Explanation: The character 'l' at index 0 is the first character that does not occur at any other index. Example 2:Input: s = "loveleetcode"Output: 2 Example 3:Input: s = "aabb"Output: -1 Explanationclass Solution { publi..
2024.10.08 -
Reverse Integer
SolutionGiven a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.Assume the environment does not allow you to store 64-bit integers (signed or unsigned). ExamplesExample 1:Input: x = 123Output: 321 Example 2:Input: x = -123Output: -321 Example 3:Input: x = 120Output: 21 Explana..
2024.10.07