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

19. Remove Nth Node From End of List (LL)

时间:2018-08-18 10:25:31      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:remove   代码   int   更新   nbsp   i++   return   nth node   pre   

 1 class Solution {
 2    public ListNode removeNthFromEnd(ListNode head, int n) {
 3        if(head == null) return head;
 4        if(head.next == null) return null;
 5        ListNode node1 = head;
 6        ListNode node2 = head;
 7        ListNode p = node1;
 8        for(int i = 0; i < n - 1; i++) {
 9            node2 = node2.next;
10        }
11        if(node2.next == null) {   //要加上这一段 要是node2刚好到tail 下面代码无法更新node1, 因为node1在head
12            return head.next;
13        }
14        while(node2.next != null) {
15            p = node1;
16            node1 = node1.next;
17            node2 = node2.next;
18        }
19        p.next = node1.next;
20        return head;
21    }
22 }

 

19. Remove Nth Node From End of List (LL)

标签:remove   代码   int   更新   nbsp   i++   return   nth node   pre   

原文地址:https://www.cnblogs.com/goPanama/p/9496015.html

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