Question
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
*Difficulty: Easy
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
My Solution
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null || n <=0) return null;
//get length
ListNode p = head;
int len = 0;
while(p != null){
len++;
p = p.next;
}
if(n >= len){
head = head.next;
return head;
}
p = head;
int count = 1;
while(count < len - n){
p = p.next;
count++;
}
if(p.next != null) p.next = p.next.next;
else return null;
return head;
}
}
2nd Solution
kind of like 2 pointers
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
ListNode back = dummy, front = dummy;
dummy.next = head;
while(n-- != 0) front = front.next;
while(front.next != null) {
front = front.next;
back = back.next;
}
back.next = back.next.next;
return dummy.next;
}