From d68e9772747a6872be72ecafef1017819113d5fd Mon Sep 17 00:00:00 2001 From: linlihong <747682928@qq.com> Date: Wed, 21 Aug 2024 16:47:36 +0800 Subject: [PATCH] 2024/8/21 Hot100 LinkedList --- .../com/cool/hot100/linkedlist/Num141.java | 31 +++++++++ .../com/cool/hot100/linkedlist/Num142.java | 64 +++++++++++++++++++ .../com/cool/hot100/linkedlist/Num21.java | 54 ++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 src/main/java/com/cool/hot100/linkedlist/Num141.java create mode 100644 src/main/java/com/cool/hot100/linkedlist/Num142.java create mode 100644 src/main/java/com/cool/hot100/linkedlist/Num21.java diff --git a/src/main/java/com/cool/hot100/linkedlist/Num141.java b/src/main/java/com/cool/hot100/linkedlist/Num141.java new file mode 100644 index 0000000..1807a70 --- /dev/null +++ b/src/main/java/com/cool/hot100/linkedlist/Num141.java @@ -0,0 +1,31 @@ +package com.cool.hot100.linkedlist; + +public class Num141 { + + /** + * Date 2024/8/21 + * DayNumber 1 + * Hard 1 + * + * @return + */ + public boolean hasCycle(ListNode head) { + if (head == null) + return false; + ListNode fastNode = head.next; + ListNode slowNode = head; + if (fastNode == null) + return false; + while (fastNode != null) { + if (fastNode.next==null){ + return false; + } + fastNode=fastNode.next.next; + slowNode = slowNode.next; + if (slowNode == fastNode) + return true; + } + return false; + } + +} diff --git a/src/main/java/com/cool/hot100/linkedlist/Num142.java b/src/main/java/com/cool/hot100/linkedlist/Num142.java new file mode 100644 index 0000000..3821966 --- /dev/null +++ b/src/main/java/com/cool/hot100/linkedlist/Num142.java @@ -0,0 +1,64 @@ +package com.cool.hot100.linkedlist; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Num142 { + + + /** + * Date 2024/8/21 + * DayNumber 2 + * Hard 2 + * + * @param head + * @return + */ + public ListNode detectCycle(ListNode head) { + + // 空间复杂度O(n) +// Map map=new HashMap<>(); +// while (head!=null){ +// if(map.containsKey(head)){ +// return head; +// } +// map.put(head,map.getOrDefault(head,0)+1); +// head=head.next; +// } +// return null; + + // 空间复杂度O(1) + if (head == null) return null; + ListNode slow = head; + ListNode fast = head; + do { + if (fast == null || fast.next == null) { + return null; + } + slow = slow.next; + fast = fast.next.next; + } while (slow != fast); + ListNode node = head; + while (slow != node) { + slow = slow.next; + node = node.next; + } + return node; + } + + @Test + public void test() { +// ListNode node=new ListNode(5,null); + ListNode node1 = new ListNode(-4, null); + ListNode node2 = new ListNode(0, node1); + ListNode node3 = new ListNode(2, node2); + ListNode node4 = new ListNode(3, node3); + node1.next = node3; + detectCycle(node4); + } + +} diff --git a/src/main/java/com/cool/hot100/linkedlist/Num21.java b/src/main/java/com/cool/hot100/linkedlist/Num21.java new file mode 100644 index 0000000..99a62cf --- /dev/null +++ b/src/main/java/com/cool/hot100/linkedlist/Num21.java @@ -0,0 +1,54 @@ +package com.cool.hot100.linkedlist; + +public class Num21 { + + + /** + * Date 2024/8/21 + * DayNumber 3 + * Hard 1 + * + * @param list1 + * @param list2 + * @return + */ + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode reNode; + if(list1==null||list2==null){ + return list1==null?list2:list1; + } + if (list1.val > list2.val) { + reNode = list2; + list2 = list2.next; + } else { + reNode = list1; + list1 = list1.next; + } + ListNode returnNode=reNode; + while (list1 != null && list2 != null) { + if (list1.val > list2.val) { + reNode.next = list2; + list2 = list2.next; + reNode = reNode.next; + + } else { + reNode.next = list1; + list1 = list1.next; + reNode = reNode.next; + + } + } + while (list1 != null) { + reNode.next = list1; + list1 = list1.next; + reNode = reNode.next; + } + while (list2 != null) { + reNode.next = list2; + list2 = list2.next; + reNode = reNode.next; + } + return returnNode; + } + +}