leetcode
1.数组和链表
剑指offer24.反转链表
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F4UNvaXt-1610945679062)(E:\BlogPhoto\image-20210118102439292.png)]
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
//把后面节点指向前一个节点
ListNode pre = null;
ListNode cur = head;
ListNode next = head; //保存下一个节点
while(cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
24.两两交换链表中的节点
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xn1qNCfW-1610945679066)(E:\BlogPhoto\image-20210118105954436.png)]
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
//如果是奇数个,则不需要交换
if(head == null || head.next == null) return head;
//如果是偶数个,则需要交换
ListNode next = head.next;
head.next = swapPairs(next.next);
next.next = head;
return next;
}
}
141.环形链表
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cl8pIMAj-1610945679069)(E:\BlogPhoto\image-20210118111325954.png)]
/**
* 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) {
if(head == null) return false;
//快慢指针
ListNode slow = head;
ListNode fast = head.next;
while(slow != fast) {
if(fast == null || fast.next == null) return false;
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}
142.环形链表II
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q5YnE2LI-1610945679074)(E:\BlogPhoto\image-20210118112819925.png)]
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(true) {
if(fast == null || fast.next == null) return null;
slow = slow.next;
fast = fast.next.next;
if(slow == fast) break;
}
fast = head;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
25.k个一组翻转链表
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NWWkiExT-1610945679076)(E:\BlogPhoto\image-20210118115142036.png)]
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
//思路和两个差不多,每k个节点组内进行翻转
if(head == null || head.next == null) return head;
ListNode tail = head;
for(int i=0; i<k; i++) {
if(tail == null) return head;
tail = tail.next;
}
ListNode newHead = reverseGroup(head,tail);
head.next = reverseKGroup(tail,k);
return newHead;
}
public ListNode reverseGroup(ListNode head, ListNode tail) {
ListNode pre = null;
ListNode cur = head;
ListNode next = head;
while(cur != tail) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
2.堆栈和队列
20.有效的括号
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dVXodbXL-1610945679078)(E:\BlogPhoto\image-20210118115723365.png)]
class Solution {
public final Map<Character,Character> map = new HashMap<Character, Character>(){{
put('[', ']'); put('{', '}'); put('(', ')'); put('?', '?');
}};
public boolean isValid(String s) {
if(s.length()>0 && !map.containsKey(s.charAt(0))) return false;
LinkedList<Character> stack = new LinkedList<Character>() {{add('?');}};
for(Character c : s.toCharArray()) {
if(map.containsKey(c)) stack.addLast(c);
else if(map.get(stack.removeLast()) != c) return false;
}
return stack.size() == 1;
}
}
232.用栈实现队列
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1Py4XlNu-1610945679079)(E:\BlogPhoto\image-20210118123347445.png)]
class MyQueue {
Stack<Integer> in;
Stack<Integer> out;
/** Initialize your data structure here. */
public MyQueue() {
in = new Stack<Integer>();
out = new Stack<Integer>();
}
/** Push element x to the back of queue. */
public void push(int x) {
in.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(!out.isEmpty()) return out.pop();
while(!in.isEmpty()) {
out.push(in.pop());
}
return out.pop();
}
/** Get the front element. */
public int peek() {
if(!out.isEmpty()) return out.peek();
while(!in.isEmpty()) {
out.push(in.pop());
}
return out.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return in.isEmpty() && out.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
225.用队列实现栈
class MyStack {
//一个队列用来入栈,一个队列用来出栈
Queue<Integer> queue1;
Queue<Integer> queue2;
/** Initialize your data structure here. */
public MyStack() {
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
}
/** Push element x onto stack. */
public void push(int x) {
queue2.offer(x);
while(!queue1.isEmpty()) {
queue2.offer(queue1.poll());
}
Queue<Integer> tmp = queue1;
queue1 = queue2;
queue2 = tmp;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue1.poll();
}
/** Get the top element. */
public int top() {
return queue1.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/