2024/8/21 Hot100 LinkedList
This commit is contained in:
parent
1f28fb2d4c
commit
d68e977274
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<ListNode,Integer> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue