class Node{
public int data
;
public Node next
;
public Node(){
}
public Node(int data
){
this.data
= data
;
}
}
class LindedList {
public Node head
;
public void addFirst(int data
) {
Node node
= new Node(data
);
if (this.head
== null
) {
this.head
= node
;
} else {
node
.next
= this.head
;
this.head
= node
;
}
}
public void addLast(int data
) {
Node node
= new Node(data
);
if (this.head
== null
) {
this.head
= node
;
} else {
Node cur
= this.head
;
while (cur
.next
!= null
) {
cur
= cur
.next
;
}
cur
.next
= node
;
}
}
private void checkIndex(int index
) {
if (index
< 0 || index
> getLength()) {
throw new IndexOutOfBoundsException("不合法");
}
}
private int getLength() {
int count
= 0;
Node cur
= this.head
;
while (cur
!= null
) {
count
++;
cur
= cur
.next
;
}
return count
;
}
private Node
searchIndex(int index
) {
int count
= 0;
Node cur
= this.head
;
while (count
< index
- 1) {
count
++;
cur
= cur
.next
;
}
return cur
;
}
public boolean addIndex(int index
, int data
) {
checkIndex(index
);
if (index
== 0) {
addFirst(data
);
return true;
}
Node cur
= searchIndex(index
);
Node node
= new Node(data
);
node
.next
= cur
.next
;
cur
.next
= node
;
return true;
}
private Node
searchPrev(int key
) {
Node prev
= this.head
;
while (prev
.next
!= null
) {
if (prev
.next
.data
== key
) {
return prev
;
}
prev
= prev
.next
;
}
return null
;
}
public void remove(int key
) {
if (this.head
.data
== key
) {
this.head
= this.head
.next
;
return;
}
Node prev
= searchPrev(key
);
if (prev
== null
) {
return;
}
Node del
= prev
.next
;
prev
.next
= del
.next
;
}
public void removeAllKey(int key
) {
Node prev
= this.head
;
Node cur
= this.head
.next
;
while (cur
!= null
) {
if (prev
.next
.data
== key
) {
prev
.next
= cur
.next
;
cur
= cur
.next
;
} else {
prev
= cur
;
cur
= cur
.next
;
}
}
if (this.head
.data
== key
) {
this.head
= this.head
.next
;
}
}
public void clear() {
this.head
= null
;
}
public Node
reverseList() {
Node prev
= null
;
Node cur
= this.head
;
Node newHead
= null
;
while (cur
!= null
) {
Node curNext
= cur
.next
;
if (curNext
== null
) {
newHead
= cur
;
}
cur
.next
= prev
;
prev
= cur
;
cur
= curNext
;
}
return newHead
;
}
public void display() {
Node cur
= this.head
;
while (cur
!= null
) {
System
.out
.print(cur
.data
+ " ");
cur
= cur
.next
;
}
System
.out
.println();
}
public void display2(Node newHead
) {
Node cur
= newHead
;
while (cur
!= null
) {
System
.out
.print(cur
.data
+ " ");
cur
= cur
.next
;
}
System
.out
.println();
}
public Node
middleNode() {
Node fast
= this.head
;
Node slow
= this.head
;
while (fast
!= null
&& fast
.next
!= null
) {
fast
= fast
.next
.next
;
slow
= slow
.next
;
}
return slow
;
}
public Node
findKthToTail(int k
) {
Node fast
= this.head
;
Node slow
= this.head
;
while (k
- 1 > 0) {
if (fast
.next
!= null
) {
fast
= fast
.next
;
k
--;
} else {
System
.out
.println("没有这个节点");
return null
;
}
}
while (fast
.next
!= null
) {
fast
= fast
.next
;
slow
= slow
.next
;
}
return slow
;
}
public Node
partition(int x
) {
Node beforeStart
= null
;
Node beforeEnd
= null
;
Node afterStart
= null
;
Node afterEnd
= null
;
Node cur
= this.head
;
while (cur
!= null
) {
Node curNext
= cur
.next
;
cur
.next
= null
;
if (cur
.data
< x
) {
if (beforeStart
== null
) {
beforeStart
= cur
;
beforeEnd
= cur
;
} else {
beforeEnd
.next
= cur
;
beforeEnd
= cur
;
}
} else {
if (afterStart
== null
) {
afterStart
= cur
;
afterEnd
= cur
;
} else {
afterEnd
.next
= cur
;
afterEnd
= cur
;
}
}
cur
= curNext
;
}
if (beforeStart
== null
) {
return afterStart
;
}
beforeEnd
.next
= afterStart
;
return beforeStart
;
}
public Node
deleteDuplicatio() {
Node newHead
= new Node(-1);
Node tmp
= newHead
;
Node cur
= this.head
;
while (cur
!= null
) {
if (cur
.next
!= null
&& cur
.data
== cur
.next
.data
) {
while (cur
.next
!= null
&& cur
.data
== cur
.next
.data
) {
cur
= cur
.next
;
}
cur
= cur
.next
;
tmp
.next
= cur
;
} else {
tmp
.next
= cur
;
tmp
= tmp
.next
;
cur
= cur
.next
;
}
}
return newHead
.next
;
}
public boolean chkPalindrom(){
Node fast
= this.head
;
Node slow
= this.head
;
if(this.head
== null
){
return false;
}
if(this.head
.next
== null
){
return true;
}
while(fast
!= null
&& fast
.next
!= null
){
fast
= fast
.next
.next
;
slow
= slow
.next
;
}
Node p
= slow
.next
;
while (p
!=null
){
Node pNext
= p
.next
;;
p
.next
= slow
;
slow
= p
;
p
= pNext
;
if(p
!= null
){
pNext
= p
.next
;
}
}
while (this.head
!= slow
) {
if (this.head
.data
!= slow
.data
) {
return false;
}
if (this.head
.next
== slow
) {
return true;
}
this.head
= this.head
.next
;
slow
= slow
.next
;
}
return true;
}
public void creatCycle(){
Node cur
= this.head
;
while (cur
.next
!= null
){
cur
= cur
.next
;
}
cur
.next
= this.head
.next
.next
;
}
public boolean hasCycle() {
Node fast
= this.head
;
Node slow
= this.head
;
while (fast
!= null
&& fast
.next
!= null
) {
fast
= fast
.next
.next
;
slow
= slow
.next
;
if (fast
== slow
) {
return true;
}
}
return false;
}
public Node
detectCycle() {
Node fast
= this.head
;
Node slow
= this.head
;
while (fast
!= null
&& fast
.next
!= null
) {
fast
= fast
.next
.next
;
slow
= slow
.next
;
if (fast
== slow
) {
break;
}
}
if (fast
== null
|| fast
.next
== null
) {
return null
;
}
fast
= this.head
;
while (fast
!= slow
) {
fast
= fast
.next
;
slow
= slow
.next
;
}
return fast
;
}
}
转载请注明原文地址: https://mac.8miu.com/read-515241.html