标签:
移除链表的倒出第n个节点。/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public class Solution { public ListNode RemoveNthFromEnd(ListNode head, int n) { if(head == null){ return null; } if(n == 1 && head.next == null){ return null; } var len = 0; var h = head; while(head != null){ head = head.next; len ++; } if(len - n < 0){ return null; } var nth = len - n + 1; var tmp = h; if(nth < len){ var c = 1; while(c < nth){ h = h.next; c ++; } h.val = h.next.val; h.next = h.next.next; }else{ while(h.next.next != null) { h = h.next; } h.next = null; } return tmp; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Remove Nth Node From End of List
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/48576041