zoukankan      html  css  js  c++  java
  • Remove Nth Node From End of List

    /**
     * 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;
            
        }
    链表想象成一条路 两个人在路上走 一次遍历所有 中间的差值卡成要找的点
  • 相关阅读:
    python练习--1、简易登录接口
    python--1、入门
    mysql数据库安装
    第八章总结
    第七章总结
    第三周总结
    第二周总结
    if、switch语句
    2章总结
    1月14日总结
  • 原文地址:https://www.cnblogs.com/wangcl-8645/p/11239691.html
Copyright © 2011-2022 走看看