链表
反转链表
javaclass Solution {public ListNode reverseList(ListNode head) {ListNode prev = null;ListNode curr = head;while (curr != null) {ListNode next = curr.next;curr.next = prev;prev = curr;
javaclass Solution {public ListNode reverseList(ListNode head) {ListNode prev = null;ListNode curr = head;while (curr != null) {ListNode next = curr.next;curr.next = prev;prev = curr;
java/*** Definition for singly-linked list.* class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/public class Solution {public boolean hasCycle(ListNode head) {ListNode fast = head, slow = head;while (fast != null && fast.next != null) {fast
两个非空的链表,表示两个非负的整数。每位数字按照逆序的方式存储,并且每个节点只能存储 一位数字。请将两个数相加,并以相同形式返回一个表示和的链表。
javaclass Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode head = null, tail = null;int carry = 0;while (l1 != null || l2 != null) {int n1 = l1 != null ?
javaclass Solution {public boolean isPalindrome(ListNode head) {List<Integer> vals = new ArrayList<Integer>();// 将链表的值复制到数组中ListNode currentNode = head;while (currentNode != null) {vals.add(currentNode.val);currentNode = currentNode.next
javaclass Solution {private ListNode frontPointer;private boolean recursivelyCheck(ListNode currentNode){if(currentNode!=null){if(!recursivelyCheck(currentNode.next)){return false;}if(currentNode.val!=frontPointer.val){return false;}frontPointer = frontPointer.next
javaclass Solution {public int removeElement(int[] nums, int val) {int n = nums.length;int left = 0;for (int right = 0; right < n; right++) {if (nums[right] != val
javaclass Solution {Map<Character, Integer> symbolValues = new HashMap<Character, Integer>() {{put('I', 1);put('V', 5);put('X', 10);put('L', 50);
javaclass Solution {public int[] plusOne(int[] digits) {for (int i = digits.length - 1; i >= 0; i--) {digits[i]++;digits[i] = digits[i] % 10;if (digits[i]
javaclass Solution {public boolean canConstruct(String ransomNote, String magazine) {for (int i = 0; i < ransomNote.length(); i++) {String c = String.valueOf(ransomNote.charAt(i));if (magazine.contains(c))
javaclass Solution {public int longestConsecutive(int[] nums) {Set<Integer> num_set = new HashSet<Integer>();for (int num : nums) {num_set.add(num);}int longestStreak = 0;for (int
javaclass Solution {public boolean isValid(String s) {int n = s.length();if (n % 2 == 1) {return false;}Map<Character, Character> pairs = new HashMap<Character, Character>()
java/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/class Solution {public int maxDepth(TreeNode root) {if (root == null) {return 0;}return 1 +
javaclass Solution {public boolean isSymmetric(TreeNode root) {return check(root, root);}public boolean check(TreeNode p, TreeNode q) {if (p == null && q == null) {return true;}
java/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/class Solution {public List<Double> averageOfLevels(TreeNode root) {List<Double> averages = new ArrayList<Double>();Queue<TreeNode>
javaclass Solution {public List<List<Integer>> levelOrder(TreeNode root) {Queue<TreeNode> queue = new LinkedList<TreeNode>();List<List<Integer>> list = new ArrayList<List<Integer>>();if (root == null) {return
javaclass Solution {int pre;int ans;public int getMinimumDifference(TreeNode root) {ans = Integer.MAX_VALUE;pre = -1;dfs(root);return ans;}public void dfs(TreeNode root) {if
javaclass Solution {List<Integer> list = new ArrayList<Integer>();public int kthSmallest(TreeNode root, int k) {dfs(root);System.out.print(list);return list.get(k - 1);}private
更新于2026-10-17
更新于2026-10-17
更新于2026-10-17