Java两种方式实现向链表中插入元素

标题:Java两种方式实现向链表中插入元素

1)方式:

  • 分是否在head处进行插入
  • 在head的前面新增一个节点,使得不需要分是否是在head处插入

2)方式一:

  • 分是否在head处进行插入
/**
	 * 分是否在head处进行插入
	 * @param head
	 * @param info
	 * @param index
	 * @return
	 */
	public ListNode insert(ListNode head,int info,int index) {
		ListNode newHead = new ListNode(info);
		if(head==null) {
			return newHead;
		}
		if(index<1) {
			throw new RuntimeException("index<1 不合法");
		}
		//若添加的节点在首部
		if(index==1) {
			int temp=head.val;
			head.val=info;
			return insert(head,temp,2);
		}
		//添加在其它地方
		int count=1;
		ListNode f=head;
		ListNode p=head;
		while(count<index) {
			p=f;//p指向f的位置
			//此处判断原因:p指向最后一个元素,f的null时,依然可以插入;
			//若此时f在向后移则Nullpoint【f=f.next =》 null.next】,故判断p是否为null
			if(p==null) { 
				throw new RuntimeException("index超过了数组的长度,插入位置不合法");
			}
			f=f.next;//f指向下一个位置
			count++;
		}
		newHead.next=f;
		p.next=newHead;
		
		return head;
	}

3)方式二:

  • 在head的前面新增一个节点,使得不需要分是否是在head处插入
	/**
	 * 在head的前面新增一个节点,使得不需要分是否是在head处插入
	 * @param head
	 * @param info
	 * @param index
	 * @return
	 */
	public ListNode insert02(ListNode head,int info,int index) {
		ListNode newHead = new ListNode(info);
		if(head==null) {
			return newHead;
		}
		if(index<1) {
			throw new RuntimeException("index<1 不合法");
		}
		
		int count=1;
		ListNode myNode=new ListNode(-1);
		myNode.next=head;
		ListNode last=myNode;
		ListNode first=head;
		while(count<index) {
			last=last.next; //last指向last的下一个位置
			//此处判断原因:last指向最后一个元素,first的null时,依然可以插入;
			//若此时first再向后移则Nullpoint【first=first.next =》 null.next】,故判断last是否为null
			if(last==null) { 
				throw new RuntimeException("index超过了数组的长度,插入位置不合法");
			}
			first=first.next;//f指向下一个位置
			count++;
		}
		newHead.next=first;
		last.next=newHead;
		
		//注意不是返回head,因为若在head处进行,插入,使得head前面放置一个新插入的元素,
		//但head依然没有动,所以返回myNode.next
		return myNode.next;
	}

完整代码如下:

/**
 * 测试添加节点向链表中
 * @author dell
 *
 */
public class TestInsertNode {
	/**
	 * 分是否在head处进行插入
	 * @param head
	 * @param info
	 * @param index
	 * @return
	 */
	public ListNode insert(ListNode head,int info,int index) {
		ListNode newHead = new ListNode(info);
		if(head==null) {
			return newHead;
		}
		if(index<1) {
			throw new RuntimeException("index<1 不合法");
		}
		//若添加的节点在首部
		if(index==1) {
			int temp=head.val;
			head.val=info;
			return insert(head,temp,2);
		}
		//添加在其它地方
		int count=1;
		ListNode f=head;
		ListNode p=head;
		while(count<index) {
			p=f;//p指向f的位置
			//此处判断原因:p指向最后一个元素,f的null时,依然可以插入;
			//若此时f在向后移则Nullpoint【f=f.next =》 null.next】,故判断p是否为null
			if(p==null) { 
				throw new RuntimeException("index超过了数组的长度,插入位置不合法");
			}
			f=f.next;//f指向下一个位置
			count++;
		}
		newHead.next=f;
		p.next=newHead;
		
		return head;
	}
	public void printAll(ListNode head) {
		if(head==null) {
			return ;
		}else {
			System.out.print(head.val+" ");
			this.printAll(head.next);
		}
	}
	
	@Test
	public void test() {
		//LinkedList,封装好了方法,直接添加在最后面,
//		LinkedList<Integer> list = new LinkedList<Integer>();
//		list.add(1);
//		list.add(2);
//		list.add(3);

		ListNode a1 = new ListNode(1);
		ListNode a2 = new ListNode(2);
		ListNode a3 = new ListNode(4);
		ListNode a4 = new ListNode(5);
		ListNode a5 = new ListNode(6);
		
		a1.next=a2;
		a2.next=a3;
		a3.next=a4;
		a4.next=a5;
		
		System.out.println("输出所有节点");
		this.printAll(a1);
		
		ListNode insert = this.insert(a1, 10, 6);
		System.out.println("\n测试添加节点:");
		this.printAll(insert);
	}
	
	/**
	 * 在head的前面新增一个节点,使得不需要分是否是在head处插入
	 * @param head
	 * @param info
	 * @param index
	 * @return
	 */
	public ListNode insert02(ListNode head,int info,int index) {
		ListNode newHead = new ListNode(info);
		if(head==null) {
			return newHead;
		}
		if(index<1) {
			throw new RuntimeException("index<1 不合法");
		}
		
		int count=1;
		ListNode myNode=new ListNode(-1);
		myNode.next=head;
		ListNode last=myNode;
		ListNode first=head;
		while(count<index) {
			last=last.next; //last指向last的下一个位置
			//此处判断原因:last指向最后一个元素,first的null时,依然可以插入;
			//若此时first再向后移则Nullpoint【first=first.next =》 null.next】,故判断last是否为null
			if(last==null) { 
				throw new RuntimeException("index超过了数组的长度,插入位置不合法");
			}
			first=first.next;//f指向下一个位置
			count++;
		}
		newHead.next=first;
		last.next=newHead;
		
		//注意不是返回head,因为若在head处进行,插入,使得head前面放置一个新插入的元素,
		//但head依然没有动,所以返回myNode.next
		return myNode.next;
	}
	@Test
	public void test02() {
		ListNode a1 = new ListNode(1);
		ListNode a2 = new ListNode(2);
		ListNode a3 = new ListNode(4);
		ListNode a4 = new ListNode(5);
		ListNode a5 = new ListNode(6);
		
		a1.next=a2;
		a2.next=a3;
		a3.next=a4;
		a4.next=a5;
		
		System.out.println("输出所有节点");
		this.printAll(a1);
		
		ListNode insert02 = this.insert02(a1, 10, 1);
		System.out.println("\n测试添加节点:");
		this.printAll(insert02);
	}
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值