19. 删除链表的倒数第N个节点——LeetCode

时间:2019-05-16 13:12:43   收藏:0   阅读:153

技术分享图片

 

心得:对于链表问题,加头指针可能简化问题

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null)
            return null;
	        ArrayList<ListNode> list=new ArrayList<>();//存放节点
	        ListNode tmp=new ListNode(0);//头指针
               ListNode rHead=tmp;
	        tmp.next=head;
	        while(tmp!=null)
	        {     
	        	list.add(tmp);
	        	tmp=tmp.next;
	        }
	         list.get(list.size()-n-1).next=list.get(list.size()-n).next;
	       return rHead.next;
	    }
}

  

 

原文:https://www.cnblogs.com/pc-m/p/10874871.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!