单链表


```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){//倒数第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 ;
        }
        //1.找到单链表的中间节点
        Node fast=this.head ;
        Node slow=this.head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //2.反转单链表的后半部分 slow 肯定是中间的节点
        Node cur= slow.next ;
        while (cur!=null){
            Node curNext=cur.next;
            cur.next=slow;
            slow=cur;
            cur=curNext ;
        }//3.开始一个从头走一个从尾走
        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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值