LeetCode19.删除链表的倒数第N个节点
题目
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public ListNode RemoveNthFromEnd(ListNode head, int n)
{
ListNode temp = new ListNode(-1);
ListNode temp1;
temp1 = head;
temp.next = head;
int length = 0;
while (temp1 != null)
{
length++;
temp1 = temp1.next;
}
length = length - n;
temp1 = temp;
while (length > 0)
{
length--;
temp1 = temp1.next;
}
temp1.next = temp1.next.next;
return temp.next;
}
}