首页 > 其他 > 详细

19. Remove Nth Node From End of List

时间:2016-08-10 00:41:33      阅读:196      评论:0      收藏:0      [点我收藏+]
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        
        ListNode t1=head;
        ListNode t2=head;
        ListNode last=null;
        for(int i=0;i<n;i++)
        {
            t1=t1.next;
        }
        
        while(t1!=null)
        {
            
            t1=t1.next;
            last=t2;
            t2=t2.next;
        }
        
        if(last==null)
        {
            //去头
            return head.next;
        }
        else
        {
            last.next=last.next.next;
        }
        return head;
    }
}

 

19. Remove Nth Node From End of List

原文:http://www.cnblogs.com/aguai1992/p/5755087.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!