链表的相关基础代码

链表代码整理

(知识点整理在另一篇博客《数据结构—链表》)(https://blog.csdn.net/weixin_53233197/article/details/128172559)

节点LinkNode

public class LinkNode {
    public int value;
    public LinkNode next;

    public String toString() {
        return "value=" + value +
                ", next=" + next ;
    }
}

头插法

方法1:

LinkDemo.java

public class LinkDemo{
	/**
     * 头插法
     * @param arr
     */
    public void headInsert(int[] arr){
        LinkNode link = null;
        for (int x : arr) {
            LinkNode w = new LinkNode();
            w.value = x;
            w.next = link;
            link = w;
        }
        System.out.println("头插法:"+link);
    }
}

Test.java

public class Test{
	public static void main(String[] args){
		int[] arr = {1,5,3,6,2,8,9,4,0,7};
		test.headInsert(arr);
	}
}

方法2:

public class LinkList{
	//链表记录器
	ListNode head = null;//记录第一个节点的地址
	public void headInsert(int value) {
		ListNode newNode = new ListNode(value);
		if(head==null) {
			head = newNode;
			return;
		}
		//先让新节点指向旧节点 防止链表被回收
		newNode.next = head;
		head = newNode;
	}
}
public class test {
	public static void main(String[] args){
		LinkList linkList = new LinkList();
		//插入数据
		linkList.headInsert(6);
		linkList.headInsert(8);
		linkList.headInsert(4);
		linkList.headInsert(2);
		linkList.headInsert(0);
		linkList.headInsert(5);
		linkList.headInsert(1);
		linkList.headInsert(3);
		System.out.println(linkList.head);
	}
}

尾插法

方法1:

LinkDemo.java

public class LinkDemo{
    /**
     * 尾插法
     * @param arr
     */
    public LinkNode tailInsert(int[] arr){
        LinkNode link = new LinkNode();
        link.value = arr[0];
        LinkNode flag = link;
        for (int i = 1; i < arr.length; i++) {
            LinkNode w = new LinkNode();
            w.value = arr[i];
            flag.next = w;
            flag = flag.next;
        }
        System.out.println("尾插法:"+link);
        return link;
    }
}

Test.java

public class Test{
	public static void main(String[] args){
		int[] arr = {1,5,3,6,2,8,9,4,0,7};
		LinkNode tailLink = test.tailInsert(arr);
	}
}

方法2:

public class LinkList{
	//链表记录器
	ListNode head = null;//记录第一个节点的地址
	public void tailInsert(int value) {
		//创建节点
		ListNode newNode = new ListNode(value);
		//生成链表
		//1、判断链表记录器有没有值
		if(head == null) {
		//链表记录器没有值就记录第一个新节点的值
			head=newNode;
			return;
		}
		
		//链表记录器有值
		ListNode indexNode = head;
		while(indexNode.next != null) {
			indexNode = indexNode.next;
		}
		indexNode.next = newNode;
		
	}
}
public class test {
	public static void main(String[] args){
		LinkList linkList = new LinkList();
		//插入数据
		linkList.tailInsert(6);
		linkList.tailInsert(8);
		linkList.tailInsert(4);
		linkList.tailInsert(2);
		linkList.tailInsert(0);
		linkList.tailInsert(5);
		linkList.tailInsert(1);
		linkList.tailInsert(8);
		System.out.println(linkList.head);
	}
}

遍历链表

LinkDemo.java

public class LinkDemo{
    /**
     * 遍历链表
     * @param link
     */
    public void printLink(LinkNode link){
        LinkNode index = link;
        while(index != null){
            System.out.print("节点值:"+index.value+"  ->");
            index = index.next;
        }
        System.out.println("\n==========================");
    }
}

判断链表是否成环

    /**
     * 判断链表是否成环
     * @param link
     * @return
     */
    public String isRingLink(LinkNode link){
        LinkNode fast = link;
        LinkNode slow = link;
        while(fast.next != null && fast != null){
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast){
                return "链表有环";
            }
        }
        return "链表没有环";

    }

反转链表

public void fanzhuan(LinkNode link){
        LinkNode pre = null;
        LinkNode next = null;
        while (link!=null){
            next = link.next;
            link.next=pre;
            pre = link;
            link = next;
        }
        link = pre;
        System.out.println("链表反转后:"+link);
    }```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值