```java
class Node{
public int data;
public Node next;
public Node(int data){
this.data=data;
}
}
public class MylIinkedList {
public Node head;
public void addFirst(int data) {
Node node = new Node(data);
node.next = head;
this.head = node;
}
public void addLast(int data) {
Node node = new Node(data);
if (this.head == null) {
this.head = node;
return;
}
Node cur = this.head;
while (cur != null) {
cur = cur.next;
}
cur.next = node;
}
public boolean contains(int key) {
Node cur = this.head;
while (cur != null) {
if (cur.data == key) {
return true;
}
cur=cur.next;
}
return false ;
}
public int size(){
int count=0;
Node cur=this.head;
while(cur!=null){
count++;
cur=cur.next;
}
return count;
}
public void addIndex( int index,int data){
Node node=new Node(data);
Node cur=searchIndex(index );
node.next=cur.next;
cur.next =node;
if(index==0){
addFirst(data) ;
return ;
}
if(index==this.size()){
addLast(data);
return ;
}
}
private Node searchIndex(int index){
if(index<0||index>this.size() ){
throw new RuntimeException("index位置不合法!");
}
Node cur=this.head;
while(index -1!=0){
cur=cur.next;
index --;
}
return cur;
}
private Node searchPrev(int key){
Node prev=this.head;
while (prev.next!=null){
if(prev.next.data==key){
return prev ;
}else{
prev=prev.next;
}
}
return null;
}
public void remove(int key){
if(this.head==null){
return ;
}
if(this.head.data==key){
this.head=this.head.next;
return ;
}
Node prev=searchPrev( key);
if(prev==null ){
System.out.println("跟本没有这个节点");
return;
}
Node del=prev.next;
prev .next=del;
}
public Node reverseList(){
Node cur=this.head;
Node prev= null;
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 removeALLKey(int val){
Node prev=this.head;
Node cur=this.head.next ;
while(cur!=null){
if(cur.data==val){
prev .next=cur.next;
cur=cur.next;
}else{
prev=cur;
cur=cur.next;
}
}
if(this.head.data==val){
this.head =this.head.next ;
}
}
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){
if(k<=0){
System.out.println("k不符合");
return null;
}
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 bs= null;
Node be= null;
Node as= null;
Node ae= null;
Node cur= this.head ;
while(cur!=null){
if(cur.data<x){
if(bs==null){
bs=cur;
be=cur;
}else{
be.next=cur;
be=be.next ;
}
}else{
if(as==null){
as=cur;
ae=cur;
}else{
ae.next =cur;
ae=ae.next;
}
}
cur=cur.next ;
}
if(bs==null){
return null;
}
be.next=as;
if(ae!=null){
ae.next=null;
}
return bs;
}
public Node deleteDuplication() {
Node newHead=new Node(-1);
Node cur=this.head;
Node tmp=newHead;
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 ;
}else {
tmp.next=cur;
tmp=tmp.next;
cur=cur.next;
}
}
tmp.next=null;
return newHead .next;
}
public boolean chkPalindrome() {
if(this.head==null){
return false ;
}
if(this.head.next==null){
return true ;
}
Node fast=this.head ;
Node slow=this.head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
Node cur= slow.next ;
while (cur!=null){
Node curNext=cur.next;
cur.next=slow;
slow=cur;
cur=curNext ;
}
while(slow!=this.head){
if(slow.data!=this.head.data){
return false ;
}
if(this.head.next ==slow ){
return true ;
}
slow=slow.next ;
this.head =this.head.next;
}
return true ;
}
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;
}
slow =this.head;
while(fast!=slow){
fast=fast.next ;
slow =slow.next ;
}
return slow;
}
public Node deda(){
Node newHead=new Node(-1) ;
Node tmp= this.head;
Node headA=this.head;
Node headB=this.head;
while (headA!=null ||headB !=null){
if(headA.data<headB .data){
tmp.next=headA ;
tmp=tmp.next;
headA =headA .next;
}else{
tmp.next=headB ;
tmp=tmp.next;
headB =headB.next;
}
}
return newHead.next ;
}
public void display() {
Node cur = this.head;
while (cur != null) {
System.out.println(cur.data+" ");
cur = cur.next;
}
}
}