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

19. Remove Nth Node From End of List

时间:2016-08-10 00:44:11      阅读:107      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!