码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Remove Nth Node From End of List

时间:2014-09-11 23:39:32      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:blog   io   java   for   div   sp   log   on   c   

public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode firstTraveling = head;
        ListNode lastTraveling = head;
        
        for (int i=0; i<n; i++) {
            firstTraveling = firstTraveling.next;
        }
        
        if (firstTraveling==null)
            return head.next;
        
        while (firstTraveling.next != null) {
            firstTraveling = firstTraveling.next;
            lastTraveling = lastTraveling.next;
        }
        
        lastTraveling.next = lastTraveling.next.next;
        
        return head;
    }
}

 

[LeetCode] Remove Nth Node From End of List

标签:blog   io   java   for   div   sp   log   on   c   

原文地址:http://www.cnblogs.com/yuhaos/p/3967375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!