add
This commit is contained in:
parent
ff9af65feb
commit
1403b5a7eb
@ -43,4 +43,21 @@ public class Q138 {
|
||||
}
|
||||
return res.next;
|
||||
}
|
||||
|
||||
public Node newCopy(Node head) {
|
||||
Map<Node, Node> 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);
|
||||
}
|
||||
}
|
||||
|
33
5.leetcode/src/com/fanxb/common/Q141.java
Normal file
33
5.leetcode/src/com/fanxb/common/Q141.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
30
5.leetcode/src/com/fanxb/common/Q21.java
Normal file
30
5.leetcode/src/com/fanxb/common/Q21.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
67
5.leetcode/src/com/fanxb/common/Q92.java
Normal file
67
5.leetcode/src/com/fanxb/common/Q92.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user