修改多使用这个栈会更快

LinkedList比Stack快的原因如下:

- 基于数组实现:Stack基于数组实现,随机访问(查找)效率更高,增删改效率较低。

  *     基于链表实现:LinkedList基于链表实现,增删改效率更高,随机访问(查找)效率较低。

**对于频繁的插入、删除操作,利用LinkedList实现栈自然比Stack快很多。**

0207
两个链表交替双指针
原理:双指针交替完长度是一样的,最后会同步
This commit is contained in:
whai 2023-12-31 22:05:09 +08:00
parent efb982a11b
commit db1b18ae63
3 changed files with 157 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package cn.whaifree.leetCode.LinkedList; package cn.whaifree.leetCode.LinkedList;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test; import org.junit.Test;
/** /**
@ -155,18 +156,18 @@ class MyLinkedList1 {
} }
} }
class ListNode{ //class ListNode{
int val; // int val;
ListNode next; // ListNode next;
//
public ListNode() { // public ListNode() {
} // }
//
public ListNode(int val, ListNode next) { // public ListNode(int val, ListNode next) {
this.val = val; // this.val = val;
this.next = next; // this.next = next;
} // }
} //}
/** /**
* 执行用时分布9ms * 执行用时分布9ms

View File

@ -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;
}
}
}

View File

@ -1,5 +1,6 @@
package cn.whaifree.leetCode.model; package cn.whaifree.leetCode.model;
public class ListNode { public class ListNode {
public int val; public int val;
@ -46,4 +47,5 @@ public class ListNode {
} }
} }
} }