来吧,每天做点Leetcode(中等难度题) 实时更新~尴尬---相信所有so easy

 

2. Add Two Numbers(费劲了。。。)

来吧,每天做点Leetcode(中等难度题) 实时更新~尴尬---相信所有so easy_第1张图片

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
          ListNode p = null;
		        ListNode newListNode=null;
		        ListNode templ1=l1;
		        ListNode templ2=l2;
		        
			       
			        	if(templ1.val+templ2.val>=10)
			        	{
			        		ListNode tempNode = new ListNode(templ1.val+templ2.val-10);
			        		if(templ1.next!=null){
			    				templ1.next.val = templ1.next.val+1;
			    				p = tempNode;
			    				newListNode=p;
			    			}
			    			else if(templ2.next!=null)
			    			{
			    				templ2.next.val = templ2.next.val+1;
			    				p = tempNode;
			    				newListNode=p;
			    			}else{
			    				p = tempNode;
			    				newListNode=p;
			    				ListNode tempNodetemp = new ListNode(1);
			    				p.next =  tempNodetemp;
			    				return newListNode;
			    			}
			        	}else{
			        		ListNode tempNode = new ListNode(templ1.val+templ2.val);
			        		p = tempNode;
		    				newListNode=p;
			        	}
			        	templ1=templ1.next;
			        	templ2=templ2.next;
			        
		        while(templ1!=null&&templ2!=null){
		        	
		        	if(templ1.val+templ2.val>=10)
		        	{
		        		ListNode tempNode = new ListNode(templ1.val+templ2.val-10);
		        		if(templ1.next!=null){
		    				templ1.next.val = templ1.next.val+1;
		    				p.next=tempNode;
		    				p=p.next;
		    			}
		    			else if(templ2.next!=null)
		    			{
		    				templ2.next.val = templ2.next.val+1;
		    				p.next=tempNode;
		    				p=p.next;
		    			}else{	
		    				p.next = tempNode;
		    				ListNode tempNodetemp = new ListNode(1);
		    				p.next.next =  tempNodetemp;
		    				return newListNode;	
		    			}
		        	}else{
		        		ListNode tempNode = new ListNode(templ1.val+templ2.val);
		        		p.next=tempNode;
		        		p=p.next;
		        	}
		        	templ1 = templ1.next;
		        	templ2 = templ2.next;
		        }
		                
		        if(templ1!=null){
		        	while(templ1.val==10)
		        	{
	    				ListNode tempNodetemp = new ListNode(0);
	    				p.next =  tempNodetemp;
	    				if(templ1.next!=null){
	    				templ1.next.val = templ1.next.val+1;
	    				p=p.next;
	    				}
	    				else{
	    					p.next =   tempNodetemp;
	    					ListNode tempNodeAppend = new ListNode(1);
		    				p.next.next =  tempNodeAppend;
		    				return newListNode;
	    				}
	    				templ1=templ1.next;
		        	}
		        	if(templ1.val!=10){
	    				p.next = templ1;
	    				return newListNode; 
		        	}
    			}
    			else if(templ2!=null)
    			{
    				while(templ2.val==10)
		        	{
	    				ListNode tempNodetemp = new ListNode(0);
	    				p.next =  tempNodetemp;
	    				if(templ2.next!=null){
	    				templ2.next.val = templ2.next.val+1;
	    				p=p.next;}
	    				else{
	    					p.next =   tempNodetemp;
	    					ListNode tempNodeAppend = new ListNode(1);
		    				p.next.next =  tempNodeAppend;
		    				return newListNode;
	    				}
	    				templ2=templ2.next;			
		        	}
    				if(templ2.val!=10){
	    				p.next =  templ2;
	    				return newListNode; 
		        	}
    			}else{
    				return newListNode;
    			}
		        return newListNode;
    }
}

3. Longest Substring Without Repeating Characters 尴尬的我来了

来吧,每天做点Leetcode(中等难度题) 实时更新~尴尬---相信所有so easy_第2张图片

class Solution {
    public int lengthOfLongestSubstring(String s) {
       	  	 int pos = 0;
	        int[] letters = new int [256];
	        for(int i=0;imax){
	        			max = auto_length;
	        		}
	        		pos = s.indexOf(Character.toString(s.charAt(i)));
	        		letters[s.charAt(i)]=0;	
	        		auto_length = 0;
	        		
	        		for(int j=0;jmax){
    			max = auto_length;
    		}
	        return max;
    }
}

19. Remove Nth Node From End of List(感觉中等题也像简单题,水题很多呀)

/**
 * 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) {
       ListNode p = head;
		        List pNodeList = new ArrayList();
		        while(p!=null){
		        	pNodeList.add(p);
		        	p=p.next;
		        }
		        int pos = pNodeList.size()-n;
 		        pNodeList.remove(pos);
		        int i=0;
		        p=head;ListNode pre = null;
		        while(pNodeList.size()>0&&pNodeList.get(i).val==p.val){
		        	pre = p;
		        	p = p.next;
		        	i++;
		        	if(i>=pNodeList.size()){
		        		break;
		        	}
		        }
		        if(i==0&&pNodeList.size()!=0){
		        	head= head.next;
		        }else if(pNodeList.size()!=0){
		        	pre.next = p.next;
		        }else{
		        	head=null;
		        }
		        return head;
    }
}

 

你可能感兴趣的:(来吧,每天做点Leetcode(中等难度题) 实时更新~尴尬---相信所有so easy)