/**
 * 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) {
        if(head==null)
            return head;
        ListNode node=new ListNode(0);
        node.next=head;
        ListNode slow=node;
        ListNode fast=node;
        for(int i=0;i<n;i++){
            fast=fast.next;
        }
        if(fast==null)
            return node.next;
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return node.next;
        
    }
链表想象成一条路 两个人在路上走 一次遍历所有 中间的差值卡成要找的点
 posted on 2019-07-24 17:45  JetaimeBeaucoup  阅读(70)  评论(0编辑  收藏  举报