java单链表+链表反转+链表合并+输出后k个节点+判断链表是否成环

链表反转+链表合并+输出链表后k个值+判断链表是否成环

链表的基础以及链表在内存中的解释,不太清楚的可以看这个链接

https://blog.csdn.net/PDyee/article/details/121704424?spm=1001.2014.3001.5501

先创建三个类

ListNode类
public class ListNode {
public int value;
public ListNode next;
 public ListNode(int value) {
	 this.value=value;
 }
}
LinkNode类
public class LinkNode {
  ListNode head=null;
  //尾插法 想要有数据可以用该方法插入数据
  public void insert(int value) {
	  ListNode node=new ListNode(value);
	  ListNode temp=head;
	  if(head==null) {
		  head=node;
		  return;
	  }
	  while(temp.next!=null) {
		  temp=temp.next;		  
	  }
	  temp.next=node;
  }
  //输出链表的值,即value值
  public void print() {
	  ListNode temp=head;
	  while(temp!=null) {
		  System.out.print(temp.value+"  ");		  
		  temp=temp.next;
	  }
	  System.out.println();
  }
	//获取长度,返回int类型数据
	public int getLength() {
	ListNode temp=head;
	if(head==null) {
		return 0;
	}
	int count=0;
	while(temp!=null) {
		temp=temp.next;
		count++;
	}
	return count;
}
    
  //链表的反转方法一
  //原理是定义两个指针,一个是pre代表head前一个节点,一个是then代表head的后一个节点,当头节点head不为空时,then成为head后一个节点,让head指向前一个节点pre,pre变为节点head,head变为then,当head为空时,头结点head变为pre节点从而保证链表反转
  public void reverse() {
	  ListNode pre=null;
	  ListNode then=null;
	  while(head!=null) {
		  then=head.next;
		  head.next=pre;
		  pre=head;
          head=then;
	  }
	 head=pre; 	  
  }
 //链表的反转方法二
 //用数组来实现链表反转,知道链表长度的情况下,把链表放入数组形成镜像链表
  public void arrReverse(LinkNode node) {
    //使用上面getLength()方法,获取长度。
	int length=node.getLength();
    //定义 ListNode类型数组。 
	ListNode arr[]=new ListNode[length];
     //将每一个ListNode装入
	for(int i=0;i<length;i++) {
		arr[i]=head;
		head=head.next;
	}
     //链表反转,最后的数组指向前一个
	for(int i=length-1;i>0;i--) {
		arr[i].next=arr[i-1];
	}
    //因为链表反转,第一个节点没有next  
	arr[0].next=null;
    //最后一个成为头结点  
	head=arr[length-1];
}  
    
    
  //链表的合并,和归并排序的 合并算法比较像,可以做参考
  public void merge(ListNode head1,ListNode head2) {
      //定义新链表node用于存储合并之后的
	  ListNode node=new ListNode(0);
      //定义新链表游标
	  ListNode temp=node;
      //当要合并的两个链表都有值时,执行
	  while(head1!=null&&head2!=null) {
          //当head1的值小时,新数组的游标指向head1,head1变为下一个链表继续比较,else则情况相反
		  if(head1.value<head2.value) {
			  temp.next=head1;
			  head1=head1.next;
		  }else {
			  temp.next=head2;
			  head2=head2.next;
		  }
          //比较完之后,游标移动
		  temp=temp.next;
	  }
      //当head1中没有节点了,将游标指向head2。
	  if(head1==null) {
		  temp.next=head2;
	  }
	  if(head2==null) {
		  temp.next=head1;
	  }
  }
  
  //获取后k个值
  public void getk(int k) {
      //定义两个游标
	  ListNode temp1=head;
	  ListNode temp2=head;
      //将游标temp1先移动k次,此时游标temp1和temp2中有k个节点。
	  while(temp1!=null&&k>0) {
		  temp1=temp1.next;
		  k--;
	  }
      //让游标temp1和游标temp2同时移动,当temp1为空时代表,temp1到达了最后,而temp1和temp2之间的为获取的后k个值
	  while(temp1!=null) {
		  temp1=temp1.next;
		  temp2=temp2.next;
	  }
      //取出后k个值
	  while(temp2!=null) {
		  System.out.print(temp2.value+" ");
		  temp2=temp2.next;
	  }
	  System.out.println();
  }
  
  //判断链表是否成环
  public boolean cycle() {
         //定义两个游标从头结点开始,一个一次走两步,一个一次走一步,如果两个游标相遇,则链表成环,如果走得快的节点为空了都没相遇,则链表不成环。
	 	 ListNode fast=head;
	 	 ListNode slow=head;
	 	 while(fast!=null&&fast.next!=null) {
            //快的走两步
	  	 	fast=fast.next.next;
            //慢的走一步 
	 	 	slow=slow.next;
            //相遇成环 
	  		if(fast==slow) {
	  			return true;
			}
	}	
	  return false;
  }
  
  
}

DemoNode类
//用于测试链表
public class DemoNode {
public static void main(String[] args) {
	LinkNode node=new LinkNode();
    //插入链表
	node.insert(0);node.insert(1);node.insert(2);node.insert(3);node.insert(4);
    //输出链表
	node.print();
    //链表反转
	node.reverse();
    //再次输出
	node.print();
    //获取链表后k个值输出
	node.getk(3);
    //输出链表是否成环,true代表成环,false代表不成环。
	System.out.println(node.cycle());
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值