您的位置:首页 > 科技 > IT业 > LCR 021

LCR 021

2024/10/6 14:23:28 来源:https://blog.csdn.net/yang_brother/article/details/142069918  浏览:    关键词:LCR 021

题目:LCR 021


解法一:计算链表长度

遍历两次,第一次获取链表长度 L(包括虚拟节点),第二次遍历到第 L-n 个节点(从虚拟节点遍历)

    public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummy = new ListNode(0, head);int L = 0;ListNode temp = dummy;while (temp != null) {L++;temp = temp.next;}temp = dummy;for (int i = 1; i < L - n; i++) {temp = temp.next;}temp.next = temp.next.next;return dummy.next;}

注意

  • 建议在头节点前添加虚拟节点,因为假如要删除的是第一个元素,就不能用前节点指向后节点的方法来删除节点,因此推荐给头节点加上前驱节点
  • while 循环条件为 temp != null,而非 temp.next != null

解法二:马拉车算法

将链表所以节点压入栈中,顺序弹出,弹出n个后,栈顶元素即为删除节点的前驱节点

    public ListNode removeNthFromEnd(ListNode head, int n) {Stack<ListNode> stack = new Stack<>();ListNode dummy = new ListNode(0, head);ListNode temp = dummy;while (temp != null) {stack.push(temp);temp = temp.next;}for (int i = 0; i < n; i++) {stack.pop();}temp = stack.peek();temp.next = temp.next.next;return dummy.next;}

注意while 循环条件为 temp != null,而非 temp.next != null


解法三:双指针

设置双指针,前指针指向第 n 个节点,后指针指向头节点,同时向后遍历,当前指针遍历到尾部时,后指针指向的即为删除节点的前驱节点

    public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummy = new ListNode(0, head);ListNode first = dummy;ListNode second = dummy;for (int i = 0; i < n; i++) {first = first.next;}while (first.next != null) {first = first.next;second = second.next;}second.next = second.next.next;return dummy.next;}

注意while 循环条件为 temp.next != null,而非 temp != null


版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com