Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbd31de7

Browse files
author
haotf
committed
feat:链表
0 parents  commitbd31de7

8 files changed

+311
-0
lines changed

‎141.环形链表.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode.cn id=141 lang=java
3+
*
4+
* [141] 环形链表
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode(int x) {
14+
* val = x;
15+
* next = null;
16+
* }
17+
* }
18+
*/
19+
classSolution {
20+
publicbooleanhasCycle(ListNodehead) {
21+
ListNodeslow =head;
22+
ListNodefast =head;
23+
24+
while (fast !=null &&fast.next !=null) {
25+
slow =slow.next;
26+
fast =fast.next.next;
27+
if (slow ==fast) {
28+
returntrue;
29+
}
30+
}
31+
returnfalse;
32+
}
33+
}
34+
// @lc code=end

‎142.环形链表-ii.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @lc app=leetcode.cn id=142 lang=java
3+
*
4+
* [142] 环形链表 II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode(int x) {
14+
* val = x;
15+
* next = null;
16+
* }
17+
* }
18+
*/
19+
classSolution {
20+
publicListNodedetectCycle(ListNodehead) {
21+
ListNodeslow =head;
22+
ListNodefast =head;
23+
24+
while (fast !=null &&fast.next !=null) {
25+
slow =slow.next;
26+
fast =fast.next.next;
27+
if (slow ==fast) {
28+
break;
29+
}
30+
}
31+
32+
// 避免出现slow == fast == head情况
33+
if (fast ==null ||fast.next ==null) {
34+
returnnull;
35+
}else {
36+
slow =head;
37+
}
38+
39+
while (slow !=fast) {
40+
slow =slow.next;
41+
fast =fast.next;
42+
}
43+
returnslow;
44+
}
45+
}
46+
// @lc code=end

‎160.相交链表.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode.cn id=160 lang=java
3+
*
4+
* [160] 相交链表
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode(int x) {
14+
* val = x;
15+
* next = null;
16+
* }
17+
* }
18+
*/
19+
classSolution {
20+
publicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB) {
21+
ListNodepa =headA;
22+
ListNodepb =headB;
23+
24+
while (pa !=pb) {
25+
if (pa ==null) {
26+
pa =headB;
27+
}else {
28+
pa =pa.next;
29+
}
30+
31+
if (pb ==null) {
32+
pb =headA;
33+
}else {
34+
pb =pb.next;
35+
}
36+
}
37+
returnpa;
38+
}
39+
}
40+
// @lc code=end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @lc app=leetcode.cn id=19 lang=java
3+
*
4+
* [19] 删除链表的倒数第 N 个结点
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode() {}
14+
* ListNode(int val) { this.val = val; }
15+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16+
* }
17+
*/
18+
classSolution {
19+
publicListNoderemoveNthFromEnd(ListNodehead,intn) {
20+
ListNodevNode =newListNode();
21+
vNode.next =head;
22+
ListNodecur =vNode;
23+
ListNodepre =vNode;
24+
25+
if (n <=0) {
26+
returnvNode.next;
27+
}
28+
29+
// 先走n步
30+
for (inti =0;i <n;i++) {
31+
cur =cur.next;
32+
}
33+
34+
// 链长小于n
35+
if (cur ==null)
36+
returnvNode.next;
37+
38+
// 开始同步走
39+
while (cur.next !=null) {
40+
pre =pre.next;
41+
cur =cur.next;
42+
}
43+
44+
// 删除倒数第n个节点
45+
if (n ==1) {
46+
pre.next =null;
47+
}else {
48+
pre.next =pre.next.next;
49+
}
50+
51+
returnvNode.next;
52+
}
53+
}
54+
// @lc code=end

‎21.合并两个有序链表.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode.cn id=21 lang=java
3+
*
4+
* [21] 合并两个有序链表
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode() {}
14+
* ListNode(int val) { this.val = val; }
15+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16+
* }
17+
*/
18+
19+
classSolution {
20+
publicListNodemergeTwoLists(ListNodelist1,ListNodelist2) {
21+
ListNodevNode =newListNode(-1);
22+
ListNodecur =vNode;
23+
24+
while (list1 !=null &&list2 !=null) {
25+
if (list1.val <list2.val) {
26+
cur.next =list1;
27+
list1 =list1.next;
28+
}else {
29+
cur.next =list2;
30+
list2 =list2.next;
31+
}
32+
cur =cur.next;
33+
34+
}
35+
ListNoderest =list1 ==null ?list2 :list1;
36+
while (rest !=null) {
37+
cur.next =rest;
38+
rest =rest.next;
39+
cur =cur.next;
40+
}
41+
returnvNode.next;
42+
}
43+
}
44+
// @lc code=end

‎23.合并k个升序链表.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
importjava.util.PriorityQueue;
2+
3+
/*
4+
* @lc app=leetcode.cn id=23 lang=java
5+
*
6+
* [23] 合并K个升序链表
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for singly-linked list.
12+
* public class ListNode {
13+
* int val;
14+
* ListNode next;
15+
* ListNode() {}
16+
* ListNode(int val) { this.val = val; }
17+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
18+
* }
19+
*/
20+
classSolution {
21+
publicListNodemergeKLists(ListNode[]lists) {
22+
if (lists.length ==0)
23+
returnnull;
24+
25+
PriorityQueue<ListNode>pq =newPriorityQueue<>(lists.length, (a,b) ->a.val -b.val);
26+
for (inti =0;i <lists.length;i++) {
27+
if (lists[i] !=null) {
28+
pq.add(lists[i]);
29+
}
30+
}
31+
ListNodevNode =newListNode(-1);
32+
ListNodecur =vNode;
33+
34+
while (pq.peek() !=null) {
35+
ListNodemin =pq.poll();
36+
cur.next =min;
37+
cur =cur.next;
38+
39+
if (min.next !=null) {
40+
pq.add(min.next);
41+
}
42+
}
43+
returnvNode.next;
44+
}
45+
}
46+
// @lc code=end

‎876.链表的中间结点.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=876 lang=java
3+
*
4+
* [876] 链表的中间结点
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode() {}
14+
* ListNode(int val) { this.val = val; }
15+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16+
* }
17+
*/
18+
classSolution {
19+
publicListNodemiddleNode(ListNodehead) {
20+
ListNodeslow =head;
21+
ListNodefast =head;
22+
23+
while (fast !=null &&fast.next !=null) {
24+
slow =slow.next;
25+
fast =fast.next.next;
26+
}
27+
28+
returnslow;
29+
}
30+
}
31+
// @lc code=end

‎ListNode.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
publicclassListNode {
2+
intval;
3+
ListNodenext;
4+
5+
ListNode() {
6+
}
7+
8+
ListNode(intval) {
9+
this.val =val;
10+
}
11+
12+
ListNode(intval,ListNodenext) {
13+
this.val =val;
14+
this.next =next;
15+
}
16+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp