首页 > 代码库 > [LeetCode] Remove Nth Node From End of List

[LeetCode] Remove Nth Node From End of List

public class Solution {    public ListNode removeNthFromEnd(ListNode head, int n) {        ListNode firstTraveling = head;        ListNode lastTraveling = head;                for (int i=0; i<n; i++) {            firstTraveling = firstTraveling.next;        }                if (firstTraveling==null)            return head.next;                while (firstTraveling.next != null) {            firstTraveling = firstTraveling.next;            lastTraveling = lastTraveling.next;        }                lastTraveling.next = lastTraveling.next.next;                return head;    }}

 

[LeetCode] Remove Nth Node From End of List