Chinaunix首页 | 论坛 | 博客
  • 博客访问: 254181
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-04-02 15:38:55

/Remove Nth Node From End of List Total Accepted: 46481 Total Submissions: 167543 My Submissions Question Solution 
//Given a linked list, remove the nth node from the end of list and return its head.
//
//For example,
//
//   Given linked list: 1->2->3->4->5, and n = 2.
//
//   After removing the second node from the end, the linked list becomes 1->2->3->5.
//Note:
//Given n will always be valid.
//Try to do this in one pass.
public class RemoveNthNodeFromEndofList {
public ListNode removeNthFromEnd(ListNode head, int n) {
      ListNode result=new ListNode(0);
       result.next=head;
       ListNode node=head;
       ListNode post=head;
       for(int i=0;i         post=post.next;
       }
       if(post==null)
        return result.next.next;
       while(post.next!=null){
        post=post.next;
        node=node.next;
       }
       node.next=node.next.next;
       return result.next;
       
}
}

阅读(412) | 评论(0) | 转发(0) |
0

上一篇:4Sum

下一篇:Valid Parentheses

给主人留下些什么吧!~~