diff --git a/5.leetcode/src/com/fanxb/common/Q138.java b/5.leetcode/src/com/fanxb/common/Q138.java index 947f82c..9566de4 100644 --- a/5.leetcode/src/com/fanxb/common/Q138.java +++ b/5.leetcode/src/com/fanxb/common/Q138.java @@ -43,4 +43,21 @@ public class Q138 { } return res.next; } + + public Node newCopy(Node head) { + Map map = new HashMap<>(); + Node cur = head; + while (cur != null) { + map.put(cur, new Node(cur.val)); + cur = cur.next; + } + cur = head; + while (cur != null) { + Node n = map.get(cur); + n.random = map.get(cur.random); + n.next = map.get(cur.next); + cur = cur.next; + } + return map.get(head); + } } diff --git a/5.leetcode/src/com/fanxb/common/Q141.java b/5.leetcode/src/com/fanxb/common/Q141.java new file mode 100644 index 0000000..bb7b36d --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q141.java @@ -0,0 +1,33 @@ +package com.fanxb.common; + +import java.util.HashMap; +import java.util.Map; + +/** + * TODO + * + * @author FleyX + * @date 2022/3/3 22:35 + */ +public class Q141 { + private static class Node { + int val; + Node next; + + public Node(int val) { + this.val = val; + this.next = null; + } + } + + public boolean hasCycle(ListNode head) { + if (head == null || head.next == null) return false; + ListNode fast = head, slow = head; + while (fast.next != null && fast.next.next != null) { + fast = fast.next.next; + slow = slow.next; + if (fast == slow) return true; + } + return false; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q2.java b/5.leetcode/src/com/fanxb/common/Q2.java index f865af8..11c63dd 100644 --- a/5.leetcode/src/com/fanxb/common/Q2.java +++ b/5.leetcode/src/com/fanxb/common/Q2.java @@ -62,17 +62,39 @@ public class Q2 { return res; } + public ListNode addTwoNumbers1(ListNode l1, ListNode l2) { + ListNode res = new ListNode(0); + ListNode temp = null; + //是否进位 + boolean jw = false; + while (l1 != null || l2 != null) { + if (temp == null) temp = res; + else { + temp.next = new ListNode(0); + temp = temp.next; + } + if (l1 != null) { + temp.val += l1.val; + l1 = l1.next; + } + if (l2 != null) { + temp.val += l2.val; + l2 = l2.next; + } + if (jw) { + temp.val += 1; + jw = false; + } + if (temp.val >= 10) { + jw = true; + temp.val -= 10; + } + } + if (jw) temp.next = new ListNode(1); + return res; + } + + public static void main(String[] args) { - ListNode l1 = new ListNode(2); - l1.next = new ListNode(4); - l1.next.next = new ListNode(9); - - ListNode l2 = new ListNode(5); - l2.next = new ListNode(6); - l2.next.next = new ListNode(4); - l2.next.next.next = new ListNode(9); - - new Q2().addTwoNumbers(l1, l2); -// new Q2().addTwoNumbers(new ListNode(0), new ListNode(0)); } } diff --git a/5.leetcode/src/com/fanxb/common/Q21.java b/5.leetcode/src/com/fanxb/common/Q21.java new file mode 100644 index 0000000..caf228b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q21.java @@ -0,0 +1,30 @@ +package com.fanxb.common; + +public class Q21 { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + if (list2 == null) return list1; + if (list1 == null) return list2; + ListNode res = null; + if (list1.val < list2.val) { + res = list1; + list1 = list1.next; + } else { + res = list2; + list2 = list2.next; + } + ListNode temp = res; + while (list1 != null && list2 != null) { + if (list1.val < list2.val) { + temp.next = list1; + list1 = list1.next; + } else { + temp.next = list2; + list2 = list2.next; + } + temp = temp.next; + } + if (list1 != null) temp.next = list1; + else temp.next = list2; + return res; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q92.java b/5.leetcode/src/com/fanxb/common/Q92.java new file mode 100644 index 0000000..79b6dc6 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q92.java @@ -0,0 +1,67 @@ +package com.fanxb.common; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Stack; + +/** + * Created with IntelliJ IDEA + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q92 { + + 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; + } + } + + public ListNode reverseBetween(ListNode head, int left, int right) { + if (left == right) { + return head; + } + //node1 left位置前一个节点,node2 right位置后一个指针 + ListNode node1 = null, leftNode = null, node2 = null, rightNode = null; + ListNode last = null, temp = head, next = null; + int count = 0; + while (temp != null) { + count++; + next = temp.next; + if (left == count) { + node1 = last; + leftNode = temp; + } + if (right == count) { + node2 = next; + rightNode = temp; + } + if (count > left && count <= right) { + temp.next = last; + } + + last = temp; + temp = next; + } + leftNode.next = node2; + if (node1 != null) { + node1.next = rightNode; + } else { + head = rightNode; + } + return head; + } +}