3.14 链表
This commit is contained in:
parent
b0caeef468
commit
5218596c0a
40
src/main/java/linkedlist/GetIntersectionNode.java
Normal file
40
src/main/java/linkedlist/GetIntersectionNode.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package linkedlist;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 题目:160. 相交链表 (getIntersectionNode)
|
||||||
|
* 描述:给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。
|
||||||
|
|
||||||
|
* 链接:https://leetcode.cn/problems/intersection-of-two-linked-lists/
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//没想出高效方法
|
||||||
|
public class GetIntersectionNode {
|
||||||
|
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||||
|
HashSet<ListNode> visited = new HashSet<ListNode>();
|
||||||
|
ListNode temp = headA;
|
||||||
|
while (temp != null) {
|
||||||
|
visited.add(temp);
|
||||||
|
temp = temp.next;
|
||||||
|
}
|
||||||
|
temp = headB;
|
||||||
|
while (temp != null) {
|
||||||
|
if (visited.contains(temp)) {
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
temp = temp.next;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
//高效方法
|
||||||
|
public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
|
||||||
|
ListNode A = headA, B = headB;
|
||||||
|
while (A != B) {
|
||||||
|
A = A != null ? A.next : headB;
|
||||||
|
B = B != null ? B.next : headA;
|
||||||
|
}
|
||||||
|
return A;
|
||||||
|
}
|
||||||
|
}
|
58
src/main/java/linkedlist/IsPalindrome.java
Normal file
58
src/main/java/linkedlist/IsPalindrome.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package linkedlist;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 题目:234. 回文链表 (isPalindrome)
|
||||||
|
* 描述:给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
|
||||||
|
|
||||||
|
* 链接:https://leetcode.cn/problems/palindrome-linked-list/
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class IsPalindrome {
|
||||||
|
public boolean isPalindrome(ListNode head) {
|
||||||
|
ListNode temp=head;
|
||||||
|
List<Integer>list=new ArrayList<>();
|
||||||
|
while (head!=null){
|
||||||
|
list.add(head.val);
|
||||||
|
head=head.next;
|
||||||
|
}
|
||||||
|
int size=list.size();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
if(list.get(i)!=list.get(size-1-i))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//快慢指针 + 链表反转
|
||||||
|
public boolean isPalindrome1(ListNode head) {
|
||||||
|
if (head == null || head.next == null) {
|
||||||
|
return true; // 空链表或只有一个节点,直接返回 true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. 快慢指针找到链表中点
|
||||||
|
ListNode slow = head;
|
||||||
|
ListNode fast = head;
|
||||||
|
while (fast != null && fast.next != null) {
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next.next;
|
||||||
|
}
|
||||||
|
ReverseList reverseList = new ReverseList();
|
||||||
|
// 2. 反转后半部分链表
|
||||||
|
ListNode secondHalf = reverseList.reverseList(slow);
|
||||||
|
|
||||||
|
// 3. 比较前后两部分链表
|
||||||
|
ListNode p1 = head;
|
||||||
|
ListNode p2 = secondHalf;
|
||||||
|
while (p2 != null) {
|
||||||
|
if (p1.val != p2.val) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
p1 = p1.next;
|
||||||
|
p2 = p2.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
10
src/main/java/linkedlist/ListNode.java
Normal file
10
src/main/java/linkedlist/ListNode.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package linkedlist;
|
||||||
|
|
||||||
|
public class ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode next;
|
||||||
|
ListNode(int x) {
|
||||||
|
val = x;
|
||||||
|
next = null;
|
||||||
|
}
|
||||||
|
}
|
21
src/main/java/linkedlist/ReverseList.java
Normal file
21
src/main/java/linkedlist/ReverseList.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package linkedlist;
|
||||||
|
/**
|
||||||
|
* 题目:206. 反转链表 (reverseList)
|
||||||
|
* 描述:给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
|
||||||
|
|
||||||
|
* 链接:https://leetcode.cn/problems/reverse-linked-list/
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class ReverseList {
|
||||||
|
public ListNode reverseList(ListNode head) {
|
||||||
|
ListNode prev = null;
|
||||||
|
ListNode curr = head;
|
||||||
|
while (curr != null) {
|
||||||
|
ListNode next = curr.next;
|
||||||
|
curr.next = prev;
|
||||||
|
prev = curr;
|
||||||
|
curr = next;
|
||||||
|
}
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user