leetcode [#234] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (11)
目录1. 题目2. 解决方案3. 注意事项
题目Given a singly linked list, determine if it is a palindrome.
解决方案1234567891011121314151617181920212223242526272829303132/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public boolean isPalindrome(ListNode head) {        boolean result = true;        if(head == null) return result;                ListNode p = head;        List<Integer> list = new Arr...阅读全文

leetcode [#242] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (10)
目录1. 题目2. 解决方案3. 注意事项
题目Given two strings s and t, write a function to determine if t is an anagram of s.
Example:s = “anagram”, t = “nagaram”, return true.s = “rat”, t = “car”, return false.
Note:You may assume the string contains only lowercase alphabets.
解决方案123456789101112131415161718192021222324252627282930public class Solution {    public static boolean isAnagram(String s, String t) {        boolean result = true;        int sLen = s.length();        int tLen = t.length();        if(sL...阅读全文

leetcode [#237] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (16)
目录1. 题目2. 解决方案3. 注意事项
题目Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
解决方案1234567891011121314/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ...阅读全文

leetcode [#24] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (19)
目录1. 题目2. 解决方案3. 注意事项
题目Given a linked list, swap every two adjacent nodes and return its head.
Example:Given 1->2->3->4, you should return the list as 2->1->4->3.
Note:Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
解决方案123456789101112131415161718192021/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution ...阅读全文

leetcode [#237] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (18)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Solve it without division and in O(n).
Notegiven [1,2,3,4], return [24,12,8,6].
解决方案123456789101112131415161718192021222324252627282930313233public class Solution {    public int[] productExceptSelf(int[] nums) {        int N = nums.length;        int[] output = new int[N];        int zero = 0;        int total ...阅读全文