Fun data structures - lists and recursion

Recursion

In essence, the original problem into smaller problems of the same

Example: an array of summing

. 1  / * 
2  
. 3      the Sum (ARR [0 ... n--. 1]) = ARR [0] + the Sum (ARR [n-...-. 1. 1]) <- less the same problem
 . 4      the Sum (ARR [ 1 ... n-1]) = arr [1] + Sum (arr [2 ... n-1]) <- less the same problem
 . 5  
. 6                              ......
 . 7      the Sum (ARR [n- -1 ... n-1]) = arr [n-1] + Sum (arr []) <- less the same problem
 . 8  
. 9  * / 
10  
. 11  public  class the Sum {
 12 is  
13 is      public  static  int SUM ( int [] ARR) {
 14          return SUM (ARR, 0 );
 15      }
 16  
. 17      // calculate all numbers in arr [l ... n) of this section and
18 is      Private  static  int SUM ( int [] ARR, int L) {
 . 19          IF (L == arr.length)      
 20 is              return 0 ; // <- solving fundamental problems    
 21 is          return ARR [L] + SUM (ARR, L + 1 ); // <- the original problem into smaller question
 22 is      }
 23 is  
24      public  static  void main (String [] args) {
 25  
26 is          int [] the nums = {1, 2,. 3,. 4,. 5 ,. 6,. 7,. 8 };
 27          System.out.println (SUM (the nums));
 28      }
 29 }

Note recursive function "macro" semantic

Recursive function is a function, a function to complete

 

Definition for singly-linked list. 1 public class ListNode {  3 public int val; 4 public ListNode next 5 6 public ListNode(int x) {

. 7 Val = X; . 8  } . 9  10  // constructor node list 11  // use arr parameter, creating a linked list, the linked list for the current ListNode the first node  12 is  public ListNode ( int [] arr) { 13 is  14  IF ( == ARR null || arr.length == 0 ) 15  the throw  new new an IllegalArgumentException ( "CAN Not BE ARR empty" ); 16  . 17  the this .val ARR = [0 ]; 18 is ListNode CUR = the this ; . 19  for ( int I = 1; i <arr.length; i ++) { 20 is cur.next = new new ListNode (ARR [I]); 21 is CUR = cur.next; 22 is  } 23 is  } 24  25  // the current node is the head node list information string  26 is  @Override 27  public String toString () { 28  29 the StringBuilder = S new new the StringBuilder (); 30 ListNode CUR = the this ; 31 is  the while (CUR =! null ) { 32 s.append (Cur.Val + "->" ); 33 is CUR = cur.next; 34  }

        /*
        for(cur = this; current != NULL; cur = cur.next){
          s.append(cur.val + "-->");
        }
        */

35         s.append("NULL");
36         return s.toString();
37     }
38 }
 1 class Solution {
 2 
 3     public ListNode removeElements(ListNode head, int val) {
 4 
 5         while(head != null && head.val == val){
 6             ListNode delNode = head;
 7             head = head.next;
 8             delNode.next = null;
 9         }
10 
11         if(head == null)
12             return head;
13 
14         ListNode prev = head;
15         while(prev.next != null){
16             if(prev.next.val == val) {
17                 ListNode delNode = prev.next;
18                 prev.next = delNode.next;
19                 delNode.next = null;
20             }
21             else
22                 prev = prev.next;
23         }
24 
25         return head;
26     }
27 
28     public static void main(String[] args) {
29 
30         int[] nums = {1, 2, 6, 3, 4, 5, 6};
31         ListNode head = new ListNode(nums);
32         System.out.println(head);
33 
34         ListNode res = (new Solution()).removeElements(head, 6);
35         System.out.println(res);
36     }
37 }

 

 1 /// Leetcode 203. Remove Linked List Elements
 2 /// https://leetcode.com/problems/remove-linked-list-elements/description/
 3 
 4 class Solution2 {
 5 
 6     public ListNode removeElements(ListNode head, int val) {
 7 
 8         while(head != null && head.val == val)
 9             head = head.next;
10 
11         if(head == null)
12             return head;
13 
14         ListNode prev = head;
15         while(prev.next != null){
16             if(prev.next.val == val)
17                 prev.next = prev.next.next;
18             else
19                 prev = prev.next;
20         }
21 
22         return head;
23     }
24 
25     public static void main(String[] args) {
26 
27         int[] nums = {1, 2, 6, 3, 4, 5, 6};
28         ListNode head = new ListNode(nums);
29         System.out.println(head);
30 
31         ListNode res = (new Solution2()).removeElements(head, 6);
32         System.out.println(res);
33     }
34 }
Solution2

 

 1 class Solution3 {
 2 
 3     public ListNode removeElements(ListNode head, int val) {
 4 
 5         ListNode dummyHead = new ListNode(-1);
 6         dummyHead.next = head;
 7 
 8         ListNode prev = dummyHead;
 9         while(prev.next != null){
10             if(prev.next.val == val)
11                 prev.next = prev.next.next;
12             else
13                 prev = prev.next;
14         }
15 
16         return dummyHead.next;
17     }
18 
19     public static void main(String[] args) {
20 
21         int[] nums = {1, 2, 6, 3, 4, 5, 6};
22         ListNode head = new ListNode(nums);
23         System.out.println(head);
24 
25         ListNode res = (new Solution3()).removeElements(head, 6);
26         System.out.println(res);
27     }
28 }
Solution3

 

 1 class Solution4 {
 2 
 3     public ListNode removeElements(ListNode head, int val) {
 4 
 5         if(head == null)
 6             return head;
 7 
 8         ListNode res = removeElements(head.next, val);
 9         if(head.val == val)
10             return res;
11         else{
12             head.next = res;
13             return head;
14         }
15     }
16 
17     public static void main(String[] args) {
18 
19         int[] nums = {1, 2, 6, 3, 4, 5, 6};
20         ListNode head = new ListNode(nums);
21         System.out.println(head);
22 
23         ListNode res = (new Solution4()).removeElements(head, 6);
24         System.out.println(res);
25     }
26 }
Solution4

 

 1 class Solution5 {
 2 
 3     public ListNode removeElements(ListNode head, int val) {
 4 
 5         if(head == null)
 6             return head;
 7 
 8         head.next = removeElements(head.next, val);
 9         return head.val == val ? head.next : head;
10     }
11 
12     public static void main(String[] args) {
13 
14         int[] nums = {1, 2, 6, 3, 4, 5, 6};
15         ListNode head = new ListNode(nums);
16         System.out.println(head);
17 
18         ListNode res = (new Solution5()).removeElements(head, 6);
19         System.out.println(res);
20     }
21 }
Solution5

 

Guess you like

Origin www.cnblogs.com/zwxo1/p/11324637.html