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

Commit a67d740

Browse files
链表算法题
1 parent 58f27c4 commit a67d740

File tree

2 files changed

+89
-26
lines changed

2 files changed

+89
-26
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.algorithm.study.demo.datastructure.graph;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* @Author: liuxun
7+
* @CreateDate: 2019年2月21日 上午10:40
8+
* @Version: 1.0
9+
*/
10+
public class Mgraph {
11+
private int v;//顶点的个数
12+
private LinkedList<Integer> adj[];//令接表
13+
14+
15+
}
Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package com.algorithm.study.demo.datastructure.linear;
22

3-
import org.omg.PortableInterceptor.INACTIVE;
4-
5-
import java.util.Arrays;
6-
import java.util.HashMap;
7-
import java.util.Map;
8-
93
/**
4+
* 求一个字符串的最大不重复子串
5+
* 单链表链表算法题
106
* @Author: liuxun
117
* @CreateDate: 2019年2月12日 下午3:43
128
* @Version: 1.0
139
*/
1410
public class Solution {
11+
12+
/**
13+
* 环形链表检测
14+
* @param head
15+
* @return
16+
*/
1517
public static ListNode detectCycle(ListNode head) {
1618
ListNode temp=head;
1719
ListNode temp2=head;
1820
while(temp!=null && temp.next.next!=null){
21+
//通过快慢指针遍历
1922
temp2=temp2.next.next;
2023
temp=temp.next;
2124
if (temp==temp2){
25+
//寻找环形入口
2226
ListNode q = head;
2327
while(temp2!=q){
2428
temp2=temp2.next;
@@ -30,35 +34,79 @@ public static ListNode detectCycle(ListNode head) {
3034
return null;
3135
}
3236

37+
/***
38+
* 单链表的前K个的逆序输出
39+
*
40+
*/
41+
public static void reversedTopK(ListNode head,int k){
42+
if (head==null || head.next==null){
43+
return;
44+
}
45+
printNode(head);
46+
int count=1;
47+
ListNode p1=head;
48+
ListNode p2=head.next;
49+
ListNode p3=null;
50+
while (count<k){
51+
p3=p2.next;
52+
p2.next=p1;
53+
p1=p2;
54+
p2=p3;
55+
count++;
56+
}
57+
head.next=null;
58+
System.out.println("执行完毕");
59+
printNode(p1);
60+
}
61+
62+
/**
63+
* 反转链表O(n)复杂度实现
64+
*/
65+
public static void reverseLinkedList(ListNode data){
66+
if (data==null || data.next==null){
67+
return;
68+
}
69+
printNode(data);
70+
ListNode p1=data;
71+
ListNode p2=data.next;
72+
ListNode p3=null;
73+
while (p2!=null){
74+
p3=p2.next;
75+
p2.next=p1;
76+
p1=p2;
77+
p2=p3;
78+
}
79+
data.next=null;
80+
data=p1;
81+
System.out.println();
82+
System.out.println("反转完毕");
83+
printNode(data);
84+
}
85+
public static void printNode(ListNode data){
86+
for (ListNode temp=data;temp!=null;temp=temp.next){
87+
System.out.print(temp.val+"->");
88+
}
89+
}
90+
3391
public static void main(String[] args) {
3492
ListNode head=new ListNode(3);
3593
ListNode head0=new ListNode(1);
3694

3795
ListNode head1=new ListNode(2);
3896
ListNode head2=new ListNode(-4);
3997

98+
// head.next=head0;
99+
// head0.next=head1;
100+
// head1.next=head2;
101+
// head2.next=head1;
102+
// ListNode c=Solution.detectCycle(head);
103+
// System.out.println(c==null?-1:1);
104+
40105
head.next=head0;
41106
head0.next=head1;
42107
head1.next=head2;
43-
head2.next=head1;
44-
// head1.next=head2;
45-
// head2.next=head3;
46-
// head3.next=head1;
47-
ListNode c=Solution.detectCycle(head);
48-
System.out.println(c==null?-1:1);
49-
50-
51-
}
52-
public boolean containsDuplicate(int[] nums) {
53-
if(nums==null || nums.length<2){
54-
return false;
55-
}
56-
Arrays.sort(nums);
57-
for(int i=0;i<nums.length-1;i++){
58-
if (nums[i]==nums[i+1]){
59-
return true;
60-
}
61-
}
62-
return false;
108+
head2.next=null;
109+
// Solution.reverseLinkedList(head);
110+
Solution.reversedTopK(head,2);
63111
}
64112
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /