递归的核心:对于一个节点来说,它只需要知道它之后的所有节点操作之后的结果就可以了。
个人总结三要素:
来自 LeetCode206

补充:
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }1,双指针/迭代
在双指针文章里。
2,递推
class Solution {
public ListNode reverseList(ListNode head) {
return reverse(null,head);
}
public ListNode reverse(ListNode pre,ListNode cur){
if(cur == null)
return pre;
ListNode temp = cur.next;
cur.next = pre;
return reverse(cur,temp);
}
}pre为前项,cur为当前指针,temp为后项。
1,递归想对于迭代,代码会更加清晰一些。
2,递归分两种
3,有关链表的题目,应该在本子上进行画图,会让思路更加清晰。
来自LeetCode 21

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val>l2.val)
{
l2.next = mergeTwoLists(l1,l2.next);
return l2;
}
else
{
l1.next = mergeTwoLists(l1.next,l2);
return l1;
}
}
}1,想到用递归做题有点难,想到后就很容易解决。
在算法(八)那篇文章里。
很重要。(不过递归算法太简单不是很重要)