随笔分类 - LeetCode
摘要:广度优先搜索+队列 class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> list = new LinkedList<>(); if (root == nul
阅读全文
摘要:广度优先搜索 class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> list = new ArrayList<>(); if (root == null){
阅读全文
摘要:广度优先搜索+队列 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; class Solution { public List<List<In
阅读全文
摘要:栈 import java.util.Iterator; import java.util.List; import java.util.Stack; class NestedIterator implements Iterator<Integer> { Stack<NestedInteger> s
阅读全文
摘要:栈 import java.util.Stack; class Solution { public String simplifyPath(String path) { /** * 如果有连续的"//"会变成空字符串,需要额外判断 */ String[] newString = path.split
阅读全文
摘要:栈 import java.util.Stack; class Solution { public int evalRPN(String[] tokens) { /** * 逆波兰表达式 * 遇到数字压入栈,遇到算符就弹出最后两个数进行计算,将结果再压入栈 * 最后剩下一个数字,就是最终结果 */
阅读全文
摘要:双指针法 class Solution { public boolean isPalindrome(ListNode head) { ListNode dummyHead = new ListNode(0, head); ListNode slow = dummyHead; ListNode fas
阅读全文
摘要:class Solution { public void reorderList(ListNode head) { ListNode dummyHead = new ListNode(0, head); ListNode slow = dummyHead; ListNode fast = dummy
阅读全文
摘要:闭合为环 class Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null || head.next == null || k == 0){ return head; } ListNode du
阅读全文
摘要:两次遍历 class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { /** * 两次遍历 * 先得到链表的长度,再正向遍历到length - n的位置,也就是待删除节点的前一个节点 */ ListNode du
阅读全文
摘要:和下一个节点交换 class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } } /** * 时间复杂度 O(1) * 空间复杂度 O
阅读全文
摘要:自顶向下(递归) class Solution { public ListNode sortList(ListNode head) { return sortList(head, null); } public ListNode sortList(ListNode head, ListNode ta
阅读全文
摘要:迭代 class Solution { public ListNode reverseKGroup(ListNode head, int k) { ListNode dummyHead = new ListNode(0, head); ListNode prev = dummyHead; ListN
阅读全文
摘要:递归 class Solution { public ListNode insertionSortList(ListNode head) { /** * 终止条件 */ if (head == null || head.next == null){ return head; } ListNode n
阅读全文
摘要:迭代 class Solution { public ListNode swapPairs(ListNode head) { ListNode dummyHead = new ListNode(-1, head); ListNode prev = dummyHead; ListNode cur =
阅读全文
摘要:迭代 class Solution { public ListNode deleteDuplicates(ListNode head) { /** * 节点数小于2不用去重 */ if (head == null || head.next == null){ return head; } /** *
阅读全文
摘要:翻转链表 class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { l1 = reverseList(l1); l2 = reverseList(l2); /** * 最后还要反转一下 */ return r
阅读全文
摘要:迭代 class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(-1); ListNode head = dummyHead; ListNo
阅读全文
摘要:迭代 class Solution { public ListNode oddEvenList(ListNode head) { if (head == null){ return head; } /** * 类似于《86. 分割链表》,奇数位置的节点不动,将偶数位置的节点全部放到最后面,原地完成
阅读全文
摘要:迭代 class Solution { public ListNode partition(ListNode head, int x) { if (head == null){ return head; } ListNode dummyHead = new ListNode(-1); dummyHe
阅读全文

浙公网安备 33010602011771号