Skip to content

Commit 36d60b3

Browse files
add java solution for linkedList
1 parent 34d0395 commit 36d60b3

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
|237|[Node in a Linked List](https://leetcode.com/problems/-node-in-a-linked-list/)| [js](./algorithms/nodeInALinkedList/nodeInALinkedList.js),[java](./algorithms/nodeInALinkedList/Solution.java) |Easy|
199199
|236|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| |Medium|
200200
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| |Easy|
201-
|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)| |Easy|
201+
|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)| [java](./algorithms/palindromeLinkedList/Solution.java) |Easy|
202202
|233|[Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)| |Medium|
203203
|232|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)| |Easy|
204204
|231|[Power of Two](https://leetcode.com/problems/power-of-two/)| |Easy|
@@ -266,7 +266,7 @@
266266
|163|[Missing Ranges](https://leetcode.com/problems/missing-ranges/) ♥ | |Medium|
267267
|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/) | |Medium|
268268
|161|[One Edit Distance](https://leetcode.com/problems/one-edit-distance/)♥ | |Medium|
269-
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | |Easy|
269+
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [java](./algorithms/intersectionOfTwoLinkedLists/Solution.java) |Easy|
270270
|159|[Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) ♥ | |Hard|
271271
|158|[Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) ♥ | |Hard|
272272
|157|[Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) ♥ | |Easy|
@@ -285,7 +285,7 @@
285285
|144|[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)| [js](./algorithms/binaryTreePreorderTraversal/binaryTreePreorderTraversal.js) |Medium|
286286
|143|[Reorder List](https://leetcode.com/problems/reorder-list/)| |Medium|
287287
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)| |Medium|
288-
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)| |Medium|
288+
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)| [java](./algorithms/linkedListCycle/Solution.java) |Medium|
289289
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)| |Hard|
290290
|139|[Word Break](https://leetcode.com/problems/word-break/)| |Medium|
291291
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)| |Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14+
/**
15+
定义两个指针, 第一轮让两个到达末尾的节点指向另一个链表的头部, 最后如果相遇则为交点(在第一轮移动中恰好抹除了长度差)
16+
两个指针等于移动了相同的距离, 有交点就返回, 无交点就是各走了两条指针的长度
17+
**/
18+
if(headA == null || headB == null) return null;
19+
ListNode pA = headA, pB = headB;
20+
// 在这里第一轮体现在pA和pB第一次到达尾部会移向另一链表的表头, 而第二轮体现在如果pA或pB相交就返回交点, 不相交最后就是null==null
21+
while(pA != pB) {
22+
pA = pA == null ? headB : pA.next;
23+
pB = pB == null ? headA : pB.next;
24+
}
25+
return pA;
26+
}
27+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public boolean hasCycle(ListNode head) {
3+
// 使用 HashSet 来判断重复
4+
Set<ListNode> seen = new HashSet<ListNode>();
5+
while (head != null) {
6+
if (!seen.add(head)) {
7+
return true;
8+
}
9+
head = head.next;
10+
}
11+
return false;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public boolean isPalindrome(ListNode head) {
11+
List<Integer> vals = new ArrayList<Integer>();
12+
13+
// 将链表的值复制到数组中
14+
ListNode currentNode = head;
15+
while (currentNode != null) {
16+
vals.add(currentNode.val);
17+
currentNode = currentNode.next;
18+
}
19+
20+
// 使用双指针判断是否回文
21+
int front = 0;
22+
int back = vals.size() - 1;
23+
while (front < back) {
24+
if (!vals.get(front).equals(vals.get(back))) {
25+
return false;
26+
}
27+
front++;
28+
back--;
29+
}
30+
return true;
31+
}
32+
}

0 commit comments

Comments
 (0)