1. 程式人生 > >LeetCode演算法19:刪除連結串列的倒數第N個節點

LeetCode演算法19:刪除連結串列的倒數第N個節點

問題
給定一個連結串列,刪除連結串列的倒數第 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;
	}
}