leetcode:删除连表倒数第N个数组

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
     public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode a=new ListNode(0);
        a.next=head;
        ListNode first = head;
        int Zlength=0;
        int NodeLength=0;
        while (true){
            if (first.next!=null){
                first=first.next;
               ++NodeLength;

            }else break;
        }
        Zlength=NodeLength-n;
        head=a;
        for (int i = 0; i <=Zlength ; i++) {
         head=head.next;
        }
        if(n==0&&n==1){
            head.next=null;
        }else{
            head.next=head.next.next;
        }
        return a.next;
    }
}

猜你喜欢

转载自blog.csdn.net/microopithecus/article/details/83987264