Java两种方式实现链表的删除,返回头结点

标题:Java两种方式实现链表的删除,返回头结点

Java两种方式实现链表的删除,返回头结点。

方式一:分是否是删除第一个节点

public ListNode del(ListNode head,int n) {
		//head==null
		if(head==null) {
			return head;
		}
		if(n<1) {
			return null;//输入不合法
		}
		//head  1   //若是再生成一个指针放在前面,则不需要分 1 和 n
		if(n==1) {
			return head.next;
		}
		//head  n
		int count=1;
		ListNode p=head;
		ListNode s=head;
		while(count<n && p!=null) {
			s=p;
			p=p.next;
			count++;
		}
		if(p!=null) {
			s.next=p.next;
		}
		return head;
	}

方式二:生成一个新的节点,放在head的前面,使得不需要分是否删除第一个节点head

public ListNode del02(ListNode head,int n) {
		//head==null
		if(head==null) {
			return head;
		}
		if(n<1) {
			return null;//输入不合法
		}
		
		ListNode newHead=new ListNode(-1);
		newHead.next=head;
		int count=1;
		ListNode p=head;
		ListNode s=newHead;
		while(count<n && p!=null) {
			s=p;
			p=p.next;
			count++;
		}
		if(p!=null) {
			s.next=p.next;
		}
		return newHead.next;
	}

完整代码如下:

/**
 * 两种方式实现链表的删除
 * @author dell
 *
 */
public class GiveLinkAndArrToDel {
	/**
	 * 链表的删除
	 * @param head
	 * @param n
	 * @return
	 */	
	public ListNode del(ListNode head,int n) {
		//head==null
		if(head==null) {
			return head;
		}
		if(n<1) {
			return null;//输入不合法
		}
		//head  1   //若是再生成一个指针放在前面,则不需要分 1 和 n
		if(n==1) {
			return head.next;
		}
		//head  n
		int count=1;
		ListNode p=head;
		ListNode s=head;
		while(count<n && p!=null) {
			s=p;
			p=p.next;
			count++;
		}
		if(p!=null) {
			s.next=p.next;
		}
		return head;
	}

	/**
	 * 删除,新生成一个节点
	 * @param head
	 * @param n
	 * @return
	 */
	public ListNode del02(ListNode head,int n) {
		//head==null
		if(head==null) {
			return head;
		}
		if(n<1) {
			return null;//输入不合法
		}
		
		ListNode newHead=new ListNode(-1);
		newHead.next=head;
		int count=1;
		ListNode p=head;
		ListNode s=newHead;
		while(count<n && p!=null) {
			s=p;
			p=p.next;
			count++;
		}
		if(p!=null) {
			s.next=p.next;
		}
		return newHead.next;
	}
	public void printAll(ListNode head) {
		if(head==null) {
			return ;
		}else {
			System.out.print(head.val+" ");
			this.printAll(head.next);
		}
	}
	
	@Test
	public void test() {
		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);
		
		int n=1;
		ListNode del = this.del02(a1, n);
		System.out.println("\n删除后");
		this.printAll(del);
	}
	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴与您进行交流。对于您的问题,我很愿意为您解答。 首先,关于单向链表的创建与元素添加,示例代码如下: ```java class Node { int val; Node next; Node(int val) { this.val = val; } } public class LinkedListDemo { public static void main(String[] args) { Node head = new Node(1); Node cur = head; for (int i = 2; i <= 5; i++) { cur.next = new Node(i); cur = cur.next; } } } ``` 以上代码创建了一个包含 5 个节点的单向链表,其中 head 节点为头结点,cur 节点为当前节点,用来指向链表中的最后一个节点。 接着,关于实现顺序表的逆置,我们可以分别使用迭代和递归两种方式: 1、迭代方式实现: ```java public static void reverseIteratively(Node head) { if (head == null || head.next == null) { return; } Node prev = null; Node cur = head; while (cur != null) { Node next = cur.next; cur.next = prev; prev = cur; cur = next; } } ``` 以上代码使用了三个指针,prev 指向当前节点的前一个节点,cur 指向当前节点,next 指向当前节点的后一个节点。每次将当前节点指向其前一个节点,然后将三个节点分别向后移动一个节点,直到 cur 节点指向尾节点为止。 2、递归方式实现: ```java public static Node reverseRecursively(Node head) { if (head == null || head.next == null) { return head; } Node newHead = reverseRecursively(head.next); head.next.next = head; head.next = null; return newHead; } ``` 以上代码使用了递归的方式,首先判断头结点是否为空或只包含一个节点,若是则直接返回头结点。然后使用递归获取链表中的最后一个节点,将其赋值给 newHead。最后将当前节点的后一个节点指向当前节点,然后将当前节点的 next 指针置为 null,然后返回 newHead。 希望以上解答能帮助到您,如果您还有其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值