Leetcode19 Remove Nth Node From End of List
Posted chason95
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode19 Remove Nth Node From End of List相关的知识,希望对你有一定的参考价值。
开始没思路,看了Solution自己写的:
/** * 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 head_previous = new ListNode(0); head_previous.next = head; ListNode fast = head_previous,slow = head_previous; //fast first to move for(int i=0;i<n+1;i++){ fast = fast.next; } //now the gap between fast and slow is n+1,maintain it while(fast!=null){ fast = fast.next; slow = slow.next; } slow.next = slow.next.next; return head_previous.next; } }
9ms,51.75%
可以了,因为和6ms答案写的一样…
以上是关于Leetcode19 Remove Nth Node From End of List的主要内容,如果未能解决你的问题,请参考以下文章