给定一个链表,删除链表的倒数第 n 个节点并返回链表的头指针
例如,
例如,
给出的链表为: , .
删除了链表的倒数第 个节点之后,链表变为.
删除了链表的倒数第 个节点之后,链表变为.
数据范围: 链表长度 ,链表中任意节点的值满足
要求:空间复杂度 ,时间复杂度
备注:
备注:
题目保证 一定是有效的
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param n int整型 * @return ListNode类 * 1.先获取链表长度,判断n 与长度的关系,如果n >5 ,则返回null * 2.通过倒数第n个节点,找出正数第 length-n+1 个节点 */ public ListNode removeNthFromEnd (ListNode head, int n) { // write code here if (head == null || n == 0) { return null; } int length = 0; ListNode cur = head; while (cur != null) { length++; cur = cur.next; } if (n > length) { return null; } int deleteNode = length - n + 1; cur = head; if (n == length) { return head.next; } int i = 2; while (cur.next != null) { if (i == deleteNode) { cur.next = cur.next.next; break; } else { cur = cur.next; } i++; } return head; } }
public ListNode removeNthFromEnd (ListNode head, int n) { // write code here if(head == null){ return null; } ListNode slow = head; ListNode fast = head; for(int i=0; i<n; i++){ fast = fast.next; } if(fast == null){ return head.next; } while(fast.next != null){ slow = slow.next; fast = fast.next; } slow.next = slow.next.next; return head; }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ public ListNode removeNthFromEnd (ListNode head, int n) { // write code here ListNode fast = head; // 慢指针用于标记被删除点 ListNode slow = head; // 被删除点的前一个节点 ListNode pre = new ListNode(-1); // 指向头结点 pre.next = head; // 利用快慢指针找到被删除点的位置 for(int i = 0;i<n;i++){ fast = fast.next; } while(fast!= null){ fast = fast.next; slow = slow.next; pre = pre.next; } // slow == head表示被删除点为头节点,删除头节点后,新的头节点为原头结点的下一节点 if(slow == head) return head.next; // 被删除点不是头结点,则将被删除点的前一节点指向被删除点的下一节点,完成指定节点的删除 pre.next = slow.next; return head; } }
public ListNode removeNthFromEnd (ListNode head, int n) { // write code here ListNode nextNode = head; List<ListNode> list = new ArrayList<>(); while(nextNode != null){ list.add(nextNode); nextNode = nextNode.next; } int len = list.size(); if(n == len){ head = head.next; }else if(n == 1){ list.get(len - 2).next = null; }else{ list.get(len - n - 1).next = list.get(len - n + 1); } return head; }
public ListNode removeNthFromEnd (ListNode head, int n) { if(head==null) return null; ListNode dummy=new ListNode(-1); dummy.next=head; ListNode fast=dummy;//fast从dummy开始走 while(n-->0){ fast=fast.next; } ListNode slow=dummy;//slow从dummy开始走 while(fast.next!=null){ fast=fast.next; slow=slow.next; } ListNode tmp=slow.next.next; slow.next=tmp; return dummy.next; }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ public ListNode removeNthFromEnd (ListNode head, int n) { // write code here //判断 head == null if (head == null) { return null; } //找到倒数第 n + 1个节点 ListNode fast = head; ListNode slow = head; while (n-- > 0 && fast != null) { fast = fast.next; } if (fast == null) { return head.next; } while (fast.next != null) { fast = fast.next; slow = slow.next; } slow.next = slow.next.next; return head; } }
public ListNode removeNthFromEnd (ListNode head, int n) { // write code here ListNode vHead = new ListNode(-1); vHead.next = head; int length = 0; for(ListNode p = head; p != null; p = p.next){ length ++; } ListNode pre = vHead; ListNode cur = head; for(int i = 0; i < length - n; i++){ pre = pre.next; cur = cur.next; } pre.next = cur.next; cur = null; return vHead.next; }
public ListNode removeNthFromEnd (ListNode head, int n) { // write code here if(head.next == null && n == 1) return null; ListNode dummy = new ListNode(0) ,pre = head , cur = head , temp = dummy; dummy.next = head; //先让右指针走n步,当右指针为null的时候说明没有倒数第n个节点 for (int i = 0; i < n; i++) { if(cur == null) return head; cur = cur.next; } //找到要删除的节点 while(cur != null){ cur = cur.next; temp = temp.next; pre = pre.next; } //进行删除 temp.next = pre.next; return dummy.next; }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ public ListNode removeNthFromEnd (ListNode head, int n) { if (n == 0) { return head; } // write code here int length = 0; ListNode temp = head; while (temp != null) { temp = temp.next; length++; } if (length == n) { return head.next; } // n一定是有效的,只需移动到倒数第n个元素之前即可 int counter = 0; temp = head; while (temp != null) { if (length - counter == n + 1) { // 找到了倒数第n个元素的前一个元素 temp.next = temp.next.next; return head; } temp = temp.next; counter++; } return head; } }
public class Solution { /** * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ public ListNode removeNthFromEnd (ListNode head, int n) { // write code here List<Integer> list = new LinkedList(); while (head != null) { list.add(head.val); head = head.next; } if (list.size() == 1) return null; list.remove(list.size() - n); ListNode sentinel = new ListNode(-1); ListNode cur = sentinel; for (int i = 0; i < list.size(); i++) { cur.next = new ListNode(list.get(i)); cur = cur.next; } return sentinel.next; } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ public ListNode removeNthFromEnd (ListNode head, int n) { if (head == null || head.next == null) { return null; } // 获取链表长度 int len = 1; ListNode tmp = head; while (tmp.next != null) { ++len; tmp = tmp.next; } // 倒数第n个节点,正着数是第(len-n+1)个节点 int targetIndex = len - n + 1; ListNode pre = head; ListNode p = head; // 若命中且为第1个则直接跳过第1个节点,并返回 if (targetIndex == 1) { p = p.next; pre = pre.next; return pre; } int count = 1; while (p.next != null) { ++count; if (count == targetIndex) { p = p.next; pre.next = p.next; p.next = null; break; } else { p = p.next; pre = p; } } return head; } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ import java.util.*; public class Solution { /** * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ public ListNode removeNthFromEnd (ListNode head, int n) { //链表为空,直接返回null if(head == null){ return null; } //定义一个新的头结点 ListNode newHead = new ListNode(-1); newHead.next = head; ListNode cur = newHead; //定义一个queue存储节点 LinkedList<ListNode> queue = new LinkedList<>(); while(cur != null){ //每次都将当前节点存储到queue最前面 queue.addFirst(cur); cur = cur.next; } ListNode temp = null; //循环n+1次得到删除节点的前一个节点 for(int i = 0;i<=n;i++){ temp = queue.getFirst(); queue.removeFirst(); } //将待删除节点的的next指向删除的下一节点 temp.next = temp.next.next; return newHead.next; } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ public ListNode removeNthFromEnd (ListNode head, int n) { // write code here // 双指针 // 考虑到会删除头结点 ListNode res = new ListNode(-1); res.next = head; // 前指针 ListNode pre = res; // 后指针 ListNode after = res; int count = 0; while(after != null){ if(count == n){ while(after.next != null){ pre = pre.next; after = after.next; } pre.next = pre.next.next; } after = after.next; ++count; } return res.next; } }