LeetCode算法19:删除链表的倒数第N个节点

版权声明:可以转载,请注明链接出处 https://blog.csdn.net/xihuanyuye/article/details/85218701

问题
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。

示例:
给定一个链表: 1->2->3->4->5, 和 n = 2.

当删除了倒数第二个节点后,链表变为 1->2->3->5.
说明:
给定的 n 保证是有效的。

思路
其实并没有特别复杂,双指针的思路解决。先用一个指针向前走n步,之后再让头部的指针跟进。
注意边界的处理。

代码

//import Utils.ListNode;
//import Utils.delwithNodeList;
import Utils.*;
//it is very strange things

public class _19RemoveNthNodeFromEndofList{
	
  public ListNode removeNthFromEnd(ListNode l,int n){		
  if (l==null) {
	return l;
	}		
//satisfy the common scene first then think about the special scene
//n < l
			
 ListNode aheadindex = null;
 ListNode delindex = null;
 ListNode tmp = null;
 ListNode result = null;
			
 if(n<1) return l;		
 //make one more space for <= 
 so that the delindex canbe ahead of the node to be del
  for (int i=0; i<n; i++) {		
  if (i==0&&aheadindex==null) {
  aheadindex = l;
  delindex = l;
  result = delindex;
    }else {				
    aheadindex = aheadindex.next;
          }
 //this can be used to find if n is longer than the length of L
 if(aheadindex == null) return result;
 //just to be the last node
 if(aheadindex != null&& aheadindex.next == null){
  result = result.next;
  return result;
    }			 	
       }	
 //this can be used to find if n is longer than the length of L
   aheadindex = aheadindex.next;		
   while (aheadindex.next!=null) {
    aheadindex = aheadindex.next;
    delindex = delindex.next;
			}
	tmp =  delindex.next;
	delindex.next = tmp.next;
	return result;
	//the variable is the start point or the enter point of an variation in memory
			}
	public static void main(String[] arg){
	
	int[] list = {1,2,3,4,5};
	
	ListNode L1 = delwithListNode.getNodelist(list);
	int n = 0;
	
	_19RemoveNthNodeFromEndofList RemoveNthNodeFromEndofList =
	 new _19RemoveNthNodeFromEndofList();
	
	ListNode L3 = RemoveNthNodeFromEndofList.removeNthFromEnd(L1,n);

	String out = delwithListNode.readList(L3);
	System.out.println(out);
	
	}
}

这里对list的处理进行了封装,用来生成listnode和打印结果

node节点定义

package Utils;
public class ListNode{
	
	int val;
	public ListNode next;
	
	ListNode(int x){
		val = x;
		}
	}

链表生成、打印

package Utils;
//import Utils.ListNode;
import Utils.ListNode;
import Utils.*;

public class delwithListNode{
	//to generate nodelist quickly
	public static ListNode getNodelist(int[] list){
		
		if (list.length<1) {
		 	return null;
		}
		
		ListNode head = null;
		ListNode temp = null;
		
		for(int i = 0;i<list.length;i++){
			
			if(temp==null){
				temp = new ListNode(list[i]);
				head = temp;
				}else{
				temp.next = new ListNode(list[i]);
				temp = temp.next;	
				}
			}
			return head;
		}

			//to print nodelist
	public static String readList(ListNode list){
	
	StringBuilder strBuilder = new StringBuilder();
	
	//System.out.println("the list is:");
	if (list == null) return strBuilder.toString();
	strBuilder.append("[");
	while(list !=null){
		//System.out.print(list.val+",");
		strBuilder.append(list.val+",");
		list = list.next;
		}
		strBuilder.append("]");
		return strBuilder.toString();
	}
	
	public static int getListlength(ListNode list){
		System.out.println("the list length is:");
		int i = 0;
		if (list == null) System.out.println("list length is "+i);
		while(list !=null){
			i++;
			list = list.next;
		}
		//System.out.println("list length is "+i);
		return i;
	}
}

猜你喜欢

转载自blog.csdn.net/xihuanyuye/article/details/85218701