标题: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);
}
}