Skip to content

Commit

Permalink
update tree
Browse files Browse the repository at this point in the history
  • Loading branch information
sanwu authored and sanwu committed Feb 26, 2023
1 parent f735b4c commit 0ac55a1
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 36 deletions.
84 changes: 81 additions & 3 deletions src/main/java/com/learning/algorithm/ALGORITHM.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 链表

### 问题分类
链表双指针:

- [环形链表](https://leetcode-cn.com/problems/linked-list-cycle/)
Expand All @@ -24,11 +25,20 @@
- [回文链表](https://leetcode-cn.com/problems/palindrome-linked-list/)
- [合并K个升序链表](https://leetcode-cn.com/problems/merge-k-sorted-lists/)

反转链表递归写法:

### 常见func


反转链表:

```java
public class Solution {


/**
* 反转链表递归写法
* @param head
* @return
*/
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
Expand All @@ -38,9 +48,76 @@ public class Solution {
head.next = null;
return res;
}

/**
* 反转链表迭代归写法
* @param head
* @return
*/
public ListNode reverseList1(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode prefixHead = new ListNode(-1);
while(head !=null) {
ListNode next = head.next;
head.next = prefixHead.next;
prefixHead.next = head;
head = next;
}
return prefixHead.next;
}
}

```

双指针
```java
class Solution {
/**
* 查找链表中点
* @param head
* @return
*/
public ListNode findMid(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode fast = head.next;
ListNode slow = head;
while(fast != null && fast.next !=null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}

/**
* 查找倒数第N个节点
* @param head
* @param n
* @return
*/
public ListNode findLastN(ListNode head, int n) {
ListNode fast = head;
while(fast != null && n >0) {
fast = fast.next;
n--;
}
if (fast == null) {
return null;
}
ListNode slow = head;
while(fast !=null) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
```


##

### 二叉树
Expand Down Expand Up @@ -526,8 +603,8 @@ public class Solution {
## 数组与字符串

### 数组
数组问题常见的解题关键字: **指针****搜索****hash表**

针对于数组的索引问题,常规的操作就是用指针、搜索、hash表问题解决

- [移除元素](https://leetcode.cn/problems/remove-element/description/)
- [有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/description/)
Expand All @@ -536,6 +613,7 @@ public class Solution {
- [螺旋矩阵](https://leetcode.cn/problems/spiral-matrix/description/)
- [螺旋矩阵 II](https://leetcode.cn/problems/spiral-matrix-ii/)
- [螺旋矩阵 III](https://leetcode.cn/problems/spiral-matrix-iii/description/)
- [旋转矩阵](https://leetcode.cn/problems/rotate-matrix-lcci/description/)
### 字符串

公共前缀问题
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/learning/algorithm/Demo2.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.learning.algorithm;

import com.learning.algorithm.basic.treeNode.TreeNode;
import com.learning.algorithm.basic.treenode.TreeNode;

import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.listNode;
package com.learning.algorithm.basic.listnode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.listNode;
package com.learning.algorithm.basic.listnode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.listNode;
package com.learning.algorithm.basic.listnode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.listNode;
package com.learning.algorithm.basic.listnode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.learning.algorithm.basic.listNode;

import java.util.List;
package com.learning.algorithm.basic.listnode;

/**
* <pre>
Expand Down Expand Up @@ -33,8 +31,22 @@ public ListNode reverseList(ListNode head) {
return newHead;
}





/**
* 迭代法
* @param head
* @return
*/
public ListNode reverseList1(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode prefixHead = new ListNode(-1);
while(head !=null) {
ListNode next = head.next;
head.next = prefixHead.next;
prefixHead.next = head;
head = next;
}
return prefixHead.next;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.listNode;
package com.learning.algorithm.basic.listnode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.listNode;
package com.learning.algorithm.basic.listnode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.learning.algorithm.basic.listnode.doublepointer;

import com.learning.algorithm.basic.listnode.ListNode;

public class FastSlowPointer {

/**
* 查找链表中点
* @param head
* @return
*/
public ListNode findMid(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode fast = head.next;
ListNode slow = head;
while(fast != null && fast.next !=null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}

/**
* 查找倒数第N个节点
* @param head
* @param n
* @return
*/
public ListNode findLastN(ListNode head, int n) {
ListNode fast = head;
while(fast != null && n >0) {
fast = fast.next;
n--;
}
if (fast == null) {
return null;
}
ListNode slow = head;
while(fast !=null) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.learning.algorithm.basic.listNode.doublePoint;
package com.learning.algorithm.basic.listnode.doublepointer;

import com.learning.algorithm.basic.listNode.ListNode;
import com.learning.algorithm.basic.listnode.ListNode;

import java.util.HashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.learning.algorithm.basic.listNode.doublePoint;
package com.learning.algorithm.basic.listnode.doublepointer;

import com.learning.algorithm.basic.listNode.ListNode;
import com.learning.algorithm.basic.listnode.ListNode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.learning.algorithm.basic.treeNode;

import com.learning.algorithm.basic.treeNode.TreeNode;
package com.learning.algorithm.basic.treenode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.learning.algorithm.basic.treeNode;

import scala.collection.mutable.Queue;
package com.learning.algorithm.basic.treenode;

import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;

import java.util.ArrayList;
import java.util.LinkedList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;

import java.util.ArrayList;
import java.util.LinkedList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;

import java.util.ArrayList;
import java.util.LinkedList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;


import javafx.util.Pair;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.learning.algorithm.basic.treeNode;
package com.learning.algorithm.basic.treenode;

/**
* <pre>
Expand Down

0 comments on commit 0ac55a1

Please sign in to comment.