From db1b18ae6314c8c83a025443e2541509a3210792 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 31 Dec 2023 22:05:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改多使用这个栈会更快 LinkedList比Stack快的原因如下: - 基于数组实现:Stack基于数组实现,随机访问(查找)效率更高,增删改效率较低。 * 基于链表实现:LinkedList基于链表实现,增删改效率更高,随机访问(查找)效率较低。 **对于频繁的插入、删除操作,利用LinkedList实现栈自然比Stack快很多。** 0207 两个链表交替双指针 原理:双指针交替完长度是一样的,最后会同步 --- .../leetCode/LinkedList/LeetCode707.java | 25 +-- .../LeetCode_0207_IntersectionNode.java | 142 ++++++++++++++++++ .../cn/whaifree/leetCode/model/ListNode.java | 2 + 3 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode_0207_IntersectionNode.java diff --git a/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode707.java b/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode707.java index 173b94f..3680095 100644 --- a/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode707.java +++ b/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode707.java @@ -1,5 +1,6 @@ package cn.whaifree.leetCode.LinkedList; +import cn.whaifree.leetCode.model.ListNode; import org.junit.Test; /** @@ -155,18 +156,18 @@ class MyLinkedList1 { } } -class ListNode{ - int val; - ListNode next; - - public ListNode() { - } - - public ListNode(int val, ListNode next) { - this.val = val; - this.next = next; - } -} +//class ListNode{ +// int val; +// ListNode next; +// +// public ListNode() { +// } +// +// public ListNode(int val, ListNode next) { +// this.val = val; +// this.next = next; +// } +//} /** * 执行用时分布9ms diff --git a/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode_0207_IntersectionNode.java b/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode_0207_IntersectionNode.java new file mode 100644 index 0000000..f309afa --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode_0207_IntersectionNode.java @@ -0,0 +1,142 @@ +package cn.whaifree.leetCode.LinkedList; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +public class LeetCode_0207_IntersectionNode { + + @Test + public void test() { + Solution1 solution = new Solution1(); + ListNode headA = ListNode.listNodeFromArray(new int[]{1, 2, 3}); + ListNode headB = ListNode.listNodeFromArray(new int[]{5}); + ListNode listNode = new ListNode(10); + + ListNode nodeA = headA; + while (nodeA.next != null) { + nodeA = nodeA.next; + } + nodeA.next = listNode; + + ListNode nodeB = headB; + while (nodeB.next != null) { + nodeB = nodeB.next; + } + nodeB.next = listNode; + + ListNode intersectionNode = solution.getIntersectionNode(headA, headB); + if (intersectionNode != null) { + System.out.println(intersectionNode.val); + } else { + System.out.println("null"); + } + + } + + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ + class Solution { + + /** + * 先找到长的,让长的指针移动到同步 + * @param headA + * @param headB + * @return + */ + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + int lengthA = getLength(headA); + int lengthB = getLength(headB); + + if (lengthB < lengthA) { + int sub = lengthA - lengthB; + ListNode indexA = headA; + ListNode indexB = headB; + for (int i = 0; i < sub; i++) { + indexA = indexA.next; + } + + while (indexA != null) { + if (indexA == indexB) { + return indexA; + } + indexA = indexA.next; + indexB = indexB.next; + } + + } else { + int sub = lengthB - lengthA; + ListNode indexA = headA; + ListNode indexB = headB; + for (int i = 0; i < sub; i++) { + indexB = indexB.next; + } + while (indexA != null) { + if (indexA == indexB) { + return indexA; + } + indexA = indexA.next; + indexB = indexB.next; + } + } + return null; + } + + int getLength(ListNode head) { + int length = 0; + ListNode index = head; + while (index != null) { + index = index.next; + length++; + } + return length; + } + } + + class Solution1 { + + /** + * 两个链表交替双指针 + * + * - + * + * @param headA + * @param headB + * @return + */ + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + + if (headA == null || headB == null) { + return null; + } + + ListNode indexA = headA; + ListNode indexB = headB; + // 链表没有公共节点的时候, indexA和b都为null,会跳出循环 + while (indexB != indexA) { + + if (indexA == null) { + indexA = headB; + }else { + indexA = indexA.next; + } + + if (indexB == null) { + indexB = headA; + }else { + indexB = indexB.next; + } + } + return indexA; + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/model/ListNode.java b/src/main/java/cn/whaifree/leetCode/model/ListNode.java index 581da65..403bafd 100644 --- a/src/main/java/cn/whaifree/leetCode/model/ListNode.java +++ b/src/main/java/cn/whaifree/leetCode/model/ListNode.java @@ -1,5 +1,6 @@ package cn.whaifree.leetCode.model; + public class ListNode { public int val; @@ -46,4 +47,5 @@ public class ListNode { } } + }