算法练习LinkedList(一)--P2
已废弃,参考–算法练习LinkedList(三): P2、P19
已废弃,参考–算法练习LinkedList(三): P2、P19
Remove Duplicates from Sorted List 2 Medium
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5 Output: 1->2->5
Example 2:
Input: 1->1->1->2->3 Output: 2->3
昨天做得有点郁闷了。‘精准原子’操作的套路还是没有掌握,写半天没完成效果。按照O(n2)的思路实现就有点暴力了。
linked-list Medium
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
可以先复习一下翻转链表的实现。链表操作两个基本场景——
已废弃,参考–算法练习LinkedList(三): P2、P19
Rotate List Medium
Given a linked list, rotate the list to the right by k places, where k
is non-negative.
Example 1:
Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL
Example 2:
Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
86. Partition List Medium
Given a linked list and a value x
, partition it such that all nodes less than x
come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example:
Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5
梳理一下思路,应该很快可以写出来这样一个架子:
Remove Duplicates from Sorted List Easy
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
直接的实现对于最后一个节点的处理不够精细,调试后完成了算法。
138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
Java中的深拷贝:对象中的对象需要重写clone方法,将所有基本类型重新clone
109. Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted linked list: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
21. Merge Two Sorted Lists Easy
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
这是个Easy级别的,有前两次的经验,一次做对,但执行效率不高
栈stack
1021. Remove Outermost Parentheses
A valid parentheses string is either empty (""), “(” + A + “)”, or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, “”, “()”, “(())()”, and “(()(()))” are all valid parentheses strings.
栈stack
1047. Remove All Adjacent Duplicates In String
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
栈stack
144. Binary Tree Preorder Traversal Medium
Given a binary tree, return the preorder traversal of its nodes’ values.
栈stack
145. Binary Tree Postorder Traversal Hard
Given a binary tree, return the postorder traversal of its nodes’ values.
栈stack
844. Backspace String Compare Easy
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
栈stack
94. Binary Tree Inorder Traversal Medium
Given a binary tree, return the inorder traversal of its nodes’ values.
Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
栈stack
496. Next Greater Element I Easy
You are given two arrays (without duplicates) nums1
and nums2
where nums1’s
elements are subset of nums2
. Find all the next greater numbers for nums1's
elements in the corresponding places of nums2
.
栈stack
42. Trapping Rain Water Hard
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
栈stack
71. simplify path Medium
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.
栈stack
225. Implement Stack using Queues Easy
232. Implement Queue using Stacks Easy
这两道提互为实现,刚好放到一起学习。既然两种数据结构可以互相实现,可以看看有什么相似之处。
栈stack
173. Binary Search Tree Iterator Medium
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next()
will return the next smallest number in the BST.
栈stack
155. Min Stack Easy
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
list of all Problems from leetcode.
简单:https://leetcode.com/problems/remove-duplicates-from-sorted-list/
简单:https://leetcode.com/problems/merge-two-sorted-lists
中等:https://leetcode.com/problems/swap-nodes-in-pairs/
中等:https://leetcode.com/problems/linked-list-cycle-ii
困难:https://leetcode.com/problems/reverse-nodes-in-k-group/