From 5218596c0aaa622f7678ba88723a1c7d7d3516fd Mon Sep 17 00:00:00 2001 From: zhangsan <646228430@qq.com> Date: Fri, 14 Mar 2025 20:12:38 +0800 Subject: [PATCH] =?UTF-8?q?3.14=20=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/linkedlist/GetIntersectionNode.java | 40 +++++++++++++ src/main/java/linkedlist/IsPalindrome.java | 58 +++++++++++++++++++ src/main/java/linkedlist/ListNode.java | 10 ++++ src/main/java/linkedlist/ReverseList.java | 21 +++++++ 4 files changed, 129 insertions(+) create mode 100644 src/main/java/linkedlist/GetIntersectionNode.java create mode 100644 src/main/java/linkedlist/IsPalindrome.java create mode 100644 src/main/java/linkedlist/ListNode.java create mode 100644 src/main/java/linkedlist/ReverseList.java diff --git a/src/main/java/linkedlist/GetIntersectionNode.java b/src/main/java/linkedlist/GetIntersectionNode.java new file mode 100644 index 0000000..271d489 --- /dev/null +++ b/src/main/java/linkedlist/GetIntersectionNode.java @@ -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 visited = new HashSet(); + 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; + } +} diff --git a/src/main/java/linkedlist/IsPalindrome.java b/src/main/java/linkedlist/IsPalindrome.java new file mode 100644 index 0000000..219e3a1 --- /dev/null +++ b/src/main/java/linkedlist/IsPalindrome.java @@ -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; + Listlist=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; + } +} diff --git a/src/main/java/linkedlist/ListNode.java b/src/main/java/linkedlist/ListNode.java new file mode 100644 index 0000000..dd18f39 --- /dev/null +++ b/src/main/java/linkedlist/ListNode.java @@ -0,0 +1,10 @@ +package linkedlist; + +public class ListNode { + int val; + ListNode next; + ListNode(int x) { + val = x; + next = null; + } +} diff --git a/src/main/java/linkedlist/ReverseList.java b/src/main/java/linkedlist/ReverseList.java new file mode 100644 index 0000000..9549180 --- /dev/null +++ b/src/main/java/linkedlist/ReverseList.java @@ -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; + } +}