Robust prove safety offer3.4- code

topic

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

Thinking

1. The first thought is to go to the end of the list, and then by the end of the k-step back. However, the node definition list see that this is a one-way linked list, only a node pointer from front to back, this can not go.

2. The node can only start from scratch traverse the list. Then we can first get the list of nodes, you can calculate how many steps are needed from front to back away. But this need to traverse the list twice

3. In order to realize only once traversed the list, we are still the same as before flipping the array, the establishment of two pointers. Away from the first pointer of the list head pointer forward traversing k-1, the second pointer always points to the first node. When a pointer to the first node of k, two forward while traversing pointers, so that pointers to ensure that two distances to be k. Then when a pointer to the end of the first node, the second pointer is the reciprocal of the k-th node.

4. Note and the potential risk of collapse. When the head of the input pointer is null, the code tries to access a null pointer memory space; input list of nodes is less than k, for loop will go forward in the chain step k-1, will also result in a null pointer; when k is of type unsigned int, the input parameters k 0, k obtained at this time for loop is not -1, but 0xFFFFFFFF (4294967295), the number of executions will be very great, causing the program to crash.

solution

/*    public class ListNode {
        int val;
        ListNode next = null;
 
        ListNode(int val) {
            this.val = val;
        }
    }*/
 
public class Solution {
 
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head == null)
            return null;
        ListNode P1=head;
        while(P1!=null && k-->0)
            Pl = P1.next;
         // If k is larger than the length of the list 
        IF (k> 0 ) {
             return  null ;
        }
        ListNode P2=head;
        while(P1!=null){
            P1=P1.next;
            P2=P2.next;
        }
        return P2;
    }
}

 

topic

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

Thinking

1. After reversing the head node of the original list is the list of the tail node, i.e., next to the node to NULL.

2 can also recursive, so that the current head of the next point to null, constantly reversed.

3. The method or use of the head plug. Construction of a new Pointer to point

solution

/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head == null || head.next == null)
            return head;
        ListNode next=head.next;
        head.next=null;
        ListNode newHead=ReverseList(next);
        next.next=head;
        return newHead;
    }
    public ListNode ReverseList(ListNode head) {
 
 
           ListNode newList = new ListNode(- 1 );
 
 
           while (head != null ) {
 
 
               ListNode next = head.next;
 
 
               head.next = newList.next;
 
 
               newList.next = head;
 
 
               head = next;
 
 
           }
 
 
         return newList.next;
 
 
    }  
}

topic

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

Thinking

1. The following list merge process, from the head of the linked list node merging two, when the value of the head node of a linked list node list head is smaller than the value of 2, so the list node 1 after the head of the list will be merged the first node. The remaining nodes, the node list head 2 is smaller than the value of the head of the list of node 1, node 2 and therefore the head of the list is the first node remaining node, this node and the previous combined tail chain good node link up. Continuously comparing the value of the head node of the two lists.


 

 

2. In order to prevent the collapse of the null pointer to access the memory space due to the need to address the list is empty when the two lists of results, when the list is empty, the combined results list is empty.

solution

/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null)
            return list2;
        if(list2==null)
            return list1;
        if(list1.val<=list2.val){
            list1.next=Merge(list1.next,list2);
            return list1;
        }
        else{
            list2.next=Merge(list1,list2.next);
            return list2;
        }
    }
}

Guess you like

Origin www.cnblogs.com/lyeeer/p/12229982.html