https://leetcode.com/problems/remove-nth-node-from-end-of-list/
리스트가 주어질 때 끝에서 n번째 노드를 제거한 후 head 반환하기
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode tempNode = head;
ListNode currNode = new ListNode(0);
ListNode ansNode = currNode;
int count = 0;
int nextCount = 0;
while(head != null)
{
count++;
head = head.next;
}
head = tempNode;
while(head != null)
{
nextCount++;
if (nextCount == count - n + 1)
{
head = head.next;
continue;
}
currNode.next = new ListNode(head.val);
head = head.next;
currNode = currNode.next;
}
return ansNode.next;
}
}