`
ouqi
  • 浏览: 41336 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

[leetcode]removeNthFromend

阅读更多

 

 public ListNode removeNthFromEnd(ListNode head, int n) {
        // Start typing your Java solution below
        // DO NOT write main() function
     ListNode fast = head;
     ListNode slow = head;
     int i = 1;
     for(;i<=n&&fast.next!=null;i++) fast = fast.next;
     if(i==n) return head.next;
     while(fast.next!=null){
         slow = slow.next;
         fast = fast.next;
     }
     slow.next = slow.next.next;
     return head;     
    }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics