10, linked list

Subject description:

Input a linked list, the linked list output reciprocal k-th node.

Problem-solving ideas: the penultimate node k, the number of positive (size-k + 1) nodes

 public ListNode FindKthToTail(ListNode head,int k) {
         if(head==null || k<=0 || k>size(head)){
           return null;
          }
       int n=size(head)-k+1;
       while(n>1){
           head=head.next;
           n--;
       }
      return head;
    }
    
    public  int size(ListNode head){
     int count=0;
     while(head!=null){
         count++;
         head=head.next;
     }
    return count;
 }

Solving two ideas: the use of two pointers, a difference of k, when the last time the back pointer to the linked list, the pointer preceding the penultimate most k nodes (to be updated)

 

Subject description:

After entering a list inverted list, the new list of the output header.

 public ListNode ReverseList(ListNode head) {
        
        if(head==null)
              return null;
             
              ListNode reversedHead=null;
              ListNode current=head;
              
              ListNode tmp=null;
              ListNode pre=null;
              
              while(current!=null){
                  
              tmp=current.next;
              current.next=pre;
                  
              if(tmp==null)
              reversedHead=current;
                  
              pre=current;
              current=tmp;
              }
              return reversedHead;

    }

 

 

Topic three:

Two monotonically increasing input list and output list after synthesis of two lists, of course, after that we need to meet synthesis list nondecreasing rules

Traversing the two lists, each value is compared one by one, moving. Add to

 public ListNode the Merge (ListNode List1, List2 ListNode) { 
         ListNode head = null ; 
         ListNode tmp   = null ; 
         ListNode Current = null ; 
         
         the while (! List1 = null && List2 =! null ) {
              IF (list1.val> list2.val) { 
                 Current = List2; 
                 List2 = current.next; 
                 tmp = Current; 
                 tmp.next = null ; // Clear inserted after node value
                 head=add(head,tmp);
             }else{
                 current=list1;
                 list1=current.next;
                 tmp=current;
                 tmp.next=null;
                 head=add(head,tmp);
             }
         }
         
         while(list1!=null){
             current=list1;
             list1=current.next;
             tmp=current;
             tmp.next=null;
             head=add(head,tmp);
         }
         
         while(list2!=null){
             current=list2;
             list2=current.next;
             tmp=current;
             tmp.next=null;
             head=add(head,tmp);
         }
         
         return head;
    }
    
    public  ListNode add(ListNode head,ListNode node){
        if(head==null){
             head=node;
        }else{
            ListNode current=head;
          while(current.next!=null){
             current=current.next;
          }
         current.next=node;
        }
        return head;
    }

 

Guess you like

Origin www.cnblogs.com/kobe24vs23/p/11335063.html