玩转数据结构——链表和递归
Posted zwxo1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了玩转数据结构——链表和递归相关的知识,希望对你有一定的参考价值。
递归
本质上,将原来的问题,转化为更小的同一问题
举例:数组求和
1 /* 2 3 Sum(arr[0...n-1]) = arr[0] + Sum(arr[1...n-1]) <-- 更小的同一问题 4 Sum(arr[1...n-1]) = arr[1] + Sum(arr[2...n-1]) <-- 更小的同一问题 5 6 ...... 7 Sum(arr[n-1...n-1]) = arr[n-1] + Sum(arr[]) <-- 更小的同一问题 8 9 */ 10 11 public class Sum 12 13 public static int sum(int[] arr) 14 return sum(arr, 0); 15 16 17 // 计算arr[l...n)这个区间内所有数字的和 18 private static int sum(int[] arr, int l) 19 if(l == arr.length) 20 return 0; // <-- 求解最基本问题 21 return arr[l] + sum(arr, l + 1); // <-- 把原问题转化成更小的问题 22 23 24 public static void main(String[] args) 25 26 int[] nums = 1, 2, 3, 4, 5, 6, 7, 8; 27 System.out.println(sum(nums)); 28 29
注意递归函数的“宏观”语意
递归函数就是一个函数,完成一个功能
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 // 链表节点的构造函数 11 // 使用arr为参数,创建一个链表,当前的ListNode为链表头结点 12 public ListNode(int[] arr) 13 14 if(arr == null || arr.length == 0) 15 throw new IllegalArgumentException("arr can not be empty"); 16 17 this.val = arr[0]; 18 ListNode cur = this; 19 for(int i = 1 ; i < arr.length ; i ++) 20 cur.next = new ListNode(arr[i]); 21 cur = cur.next; 22 23 24 25 // 以当前节点为头结点的链表信息字符串 26 @Override 27 public String toString() 28 29 StringBuilder s = new StringBuilder(); 30 ListNode cur = this; 31 while(cur != null) 32 s.append(cur.val + "->"); 33 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
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
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
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
以上是关于玩转数据结构——链表和递归的主要内容,如果未能解决你的问题,请参考以下文章