单链表+环

1.链表的创建,(头插,尾插,/任意位置插入)
2. 删除单链表中所有值为val的值; O(n)的时间
3. 得到表长度
4. 创建环:
5. 求环的入口点:
6. 求环的长度:
7. 判断是否有环
8. **单链表的逆置?(使用头插法逆置)
9. 反转

class Link {

    class Entry{
        int data;
        Entry next;

        public Entry(){
            this.data=-1;
            this.next=null;
        }
        public Entry(int val){
            this.data=val;
            this.next=null;
        }
    }
    private Entry head;

    public Entry getHead() {
        return head;
    }


    public Link(){
        this.head=new Entry();
    }
    //头插
    public void insertHead(int val){
        Entry cur=new Entry(val);
        cur.next=this.head.next;
        this.head.next=cur;
    }
    //尾插
    public void insertTail(int val){
        Entry cur=new Entry(val);
        Entry entry=this.head;
        while (entry.next!=null){
            entry=entry.next;
        }
        entry.next=cur;
    }
    //任意位置插入
    public void insertPos(int val,int pos) {
        int count=0;
        Entry cur=new Entry(val);
        Entry entry=this.head;
        while (entry.next != null) {
            count++;
            if (count == pos) {
                cur.next=entry.next;
                entry.next=cur;
                break;
            }
            entry=entry.next;
        }
    }
    //删除单链表中所有值为val的值; O(n)的时间
    public void delectVal(int val){
        Entry entry=this.head;
        while (entry.next!=null){
            if (entry.next.data==val){
                entry.next=entry.next.next;
            }else{
                entry=entry.next;
            }
        }
    }

    //得到表长度
    public int getLength(){
        int count=0;
        Entry entry=this.head.next;
        while (entry!=null){
            count++;
            entry=entry.next;
        }
        return count;
    }
     //打印
   public void show(){
        Entry entry=this.head.next;
        while (entry!=null){
            System.out.print(entry.data+" ");
            entry=entry.next;
        }
       System.out.println();
   }

   public int finK(int k){
        int length=getLength();
        Entry cur1=this.head;
        Entry cur2=this.head;
        if (k<=0||k>length){
            return -99;
        }else {
            for (int i=0; i <k-1 ; i++) {
                cur1=cur1.next;
            }
            while (cur1.next!=null){
                cur1=cur1.next;
                cur2=cur2.next;
            }
            return cur2.data;
        }
   }
   //求环的入口点:
   public int enterEntry(){
            Entry fast= this.head;
            Entry slow= this.head;
            while (fast!=null&&fast.next!=null) {
                fast=fast.next.next;
                slow=slow.next;
                if (fast==slow){
                 break;
                }
            }
            slow=this.head;
            while (fast!=slow){
                slow=slow.next;
                fast=fast.next;
            }
       return slow.data;
   }

   //求环的长度:
   public int enterEntryLength(){
       Entry fast= this.head;
       Entry slow= this.head;
       boolean flg=false;
       int count=0;
       while (fast!=null&&fast.next!=null) {
           fast=fast.next.next;
           slow=slow.next;
           if (fast==slow){
               if (flg==true){ break; }
               else { flg=true; }
           }
           if (flg==true){
               count++;
           }
       }
      return count;
   }
   //删除倒数第K节点
    public void delectFinK(int k){
        int length=getLength();
        Entry cur1=this.head;
        Entry cur2=this.head;
        if (k<=0||k>length){
            throw new UnsupportedOperationException("不合法");
        }else {
            for (int i=0; i <k ; i++) {
                cur1=cur1.next;
            }
            while (cur1.next!=null){
                cur1=cur1.next;
                cur2=cur2.next;
            }
            Entry del=cur2.next;
            cur2=del.next;
        }
    }
    //判断是否有环
    public  boolean isLoop(){
        Entry fast= this.head;
        Entry slow= this.head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if (fast==slow){
                return true;
            }
        }
        return  false;
    }
//单链表的逆置?(使用头插法逆置)
    public void reverseLinkInsertHead(){
        Entry cur1=this.head.next;
        Entry cur2=cur1.next;
        while (cur2!=null){
            cur1.next=cur2.next;
           Entry cur=cur2;
           cur.next=this.head.next;
          this.head.next=cur;
          cur2=cur1.next;
        }
    }
    //反转
    public Entry reversion(){
        Entry entry=null;
        Entry prev=null;
        Entry cur=this.head;
        Entry curNext=cur.next;
        while (curNext!=null){
            cur.next=prev;
            prev=cur;
            cur=curNext;
           curNext=curNext.next;
        }
        if (curNext==null){
            entry=cur;
        }
        return entry;
    }


  public boolean delectEntey(Entry entryStart,Entry entryDelete){
        if (entryDelete.next==null){
            if (this.head.next==entryDelete){
                this.head.next=null;
                return true;
            }else {
                Entry entry=this.head;
                while (entry.next!=null){
                    entry=entry.next;
                    if (entry.next==entryDelete){
                        entry.next=null;
                        return true;
                    }
                }
            }
        }else {
            entryDelete.data=entryDelete.next.data;
            entryDelete.next=entryDelete.next.next;
            entryDelete.next.next=null;
            return true;
        }
        return false;
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值