资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Java

单向链表操作【单链表revert,求取倒数第k个node】

Java 更新时间: 发布时间: 计算机考试归档 最新发布

单向链表操作【单链表revert,求取倒数第k个node】

package com.algorithm.collections;

public class LinkedArrayListDemo {

public static void main(String args[]){
    MyLinkedList my = new MyLinkedList();
    HeroNode h1 = new HeroNode(1,"A",null);
    HeroNode h2 = new HeroNode(2,"B",null);
    HeroNode h3= new HeroNode(3,"C",null);
    HeroNode h4 = new HeroNode(4,"D",null);

    my.add(h1);
    my.add(h2);
    my.add(h3);
    my.add(h4);
    my.print();
    HeroNode hn = my.getKthNode(4);
    if(hn !=null)
    System.out.println(hn.getName()+":" + hn.getNum());

// HeroNode reverts = my.revert();
// reverts.print(reverts);

}

}

class MyLinkedList{
HeroNode head;
public MyLinkedList(){
head = new HeroNode(0,“start”, null);
}

**public void add(HeroNode node){**
    HeroNode tmpHead = head;
    while(tmpHead.next !=null){
        tmpHead = tmpHead.next;
    }
    tmpHead.next =node;
}

**public void del(HeroNode node){**
    HeroNode tmpHead = head;
    while(tmpHead.next !=null){
        if(tmpHead.next.getNum() == node.getNum()){
            tmpHead.next = tmpHead.next.next;
            break;
        }
        tmpHead = tmpHead.next;
    }
}

**public HeroNode getKthNode(int k)**{
    if(k<=0)
    {
        System.out.println("not correct index,");
        return null;
    }
    HeroNode hn =head.next;
    HeroNode hnKthBefore =null;
    boolean flag = false;
    int i=1;
    while(hn !=null){
        if(i == k){
            hnKthBefore = head.next; //初始化
            flag=true;
        }
        if(i>k){
            hnKthBefore = hnKthBefore.next;

        }
        i=i+1;
        hn = hn.next;

    }
    System.out.println("i="+i);
    System.out.println("k="+k);
    if(i<=k && flag ==false){
        System.out.println("not correct index, it is bigger than the length: "+i);
    }
    return hnKthBefore;
}

**public HeroNode revert()**{
    HeroNode revertHead = head;
    HeroNode tmp1 = null;
    HeroNode tmp2 = head.next;
    if(head.next !=null && head.next.next ==null )
        return head;
    while(tmp2 !=null){
        HeroNode tmp3 = tmp2.next;
        tmp2.next = tmp1;
        tmp1 = tmp2;
        tmp2 = tmp3;
    }
    revertHead.next = tmp1;
    return revertHead;
}
public void print(){
    HeroNode hn = head;
   hn.print(hn);
}

}
class HeroNode{
private int num;
private String name;
public HeroNode next;

public HeroNode(int num, String name, HeroNode next){
    this.num = num;
    this.name = name;
    this.next = next;

}

public int getNum() {
    return num;
}

public void setNum(int num) {
    this.num = num;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public void print(HeroNode hn){
    while(hn !=null){
        System.out.print("["+hn.getNum()+"-"+hn.getName()+"] => ");
        hn = hn.next;
    }
}

}

转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/1042501.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【单向链表操作【单链表revert,求取倒数第k个node】】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2