根據LeetCode初步瞭解遞歸算法
//listNode
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
// 鏈表節點的構造函數
//使用arr爲參數,建立一個鏈表,當前的ListNode爲鏈表頭結點
publc ListBide(int[] arr){
if(arr == null || arr.length == 0 ){
throw new IllegalArgumentException("arr can not be empty");
this.val = arr[0];
ListNode cur = this;
for(int i = 1;i < arr.length; i++){
cur.next = new ListNode(arr[i]);
cur = cur.next;
}
}
}
// 以當前節點爲頭結點的鏈表信息字符串
@Override
public String toString(){
StringBuilder res = new StringBuilder();
ListNode cur = this;
while(cur != null){
res.append(cur.val + "->");
cur = cur.next;
}
res.append("NULL");
return res.toString();
}
}
//主代碼
public class Solution {
public ListNode removeElements(ListNode head,int val){
while(head != null && head.val == val){
ListNode delNode = head;
head = head.next;
delNode.next = null;
}
if(head == null){
return null;
}
ListNode prev = head;
while(prev.next != null){
if(prev.next.val == val){
ListNode delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
}else{
prev = prev.next;
}
}
return head;
}
}
//虛擬頭結點寫法
public class Solution {
public ListNode removeElements(ListNode head,int val){
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode prev = dummyHead;
while(prev.next != null){
if(prev.next.val == val){
ListNode delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
}else{
prev = prev.next;
}
}
return dummyHead.next;
}
}
複製代碼
遞歸
public class Sum{
public static int sum(int[] arr){
return sum(arr,0);
}
//計算arr[1...n)這個區間內全部數字的和]
private static int sum(int[] arr,int l){
if(l == arr.length){
return 0;
}
return arrp[l] + sum(arr,l + 1);
}
public static void main(String[] args){
int[] nums = {1,2,3,4,5,6,7,8};
System.out.println(sum(nums));
}
}
複製代碼
鏈表和遞歸
class Solution3{
public ListNode removeElements(ListNode head,int val){
if(head == null){
return null;
}
ListNode res = removeElements(head.next,val);
if(head.val == val){
return res;
}else{
head.next = res;
return head;
}
<!-- if(head == null){
return null;
}
head.next = removeElements(head.next,val);
return head.val == val ? head.next : head;
-->
}
public static void main(String[] args){
int[] nums = {1,2,6,3,4,5,6};
ListNode head = new ListNode(nums);
System.out.println(head);
ListNode res = (new Solution3()).removeElements(head,6);
System.out.println(res);
}
}
複製代碼
遞歸函數的「微觀」解讀
//主代碼
public class Solution {
public ListNode removeElements(ListNode head,int val,int depth){
String depthString = generateDepthString(depth);
System.out.print(depthString);
System.out.print("Call: remove "+ val + " in " + head);
if(head == null){
System.out.print(depthString);
return head;
}
head.next = removeElements(head.next,val,depth + 1);
System.out.print(depthString);
System.out.print("After: remove "+ val + " in " + res);
ListNode ret;
if(head.val == val){
ret = res;
}else{
head.next = res;
ret = head;
}
System.out.println(depthString);
return ret;
}
private String generateDepthString(int depth){
StringBuilder res = new StringBuilder();
for(int i = 0; i< depth; i++){
res.append("--");
}
return res.toString();
}
複製代碼