链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { if(pHead1 == NULL){ return pHead2; } if(pHead2 == NULL){ return pHead1; } if(pHead1->val <= pHead2->val){ pHead1->next = Merge(pHead1->next, pHead2); return pHead1; }else{ pHead2->next = Merge(pHead1, pHead2->next); return pHead2; } }
|
链表递归,重排两个有序链表。
思路:若某个链表为空,直接返回另一个链表;若不为空,比较两个链表大小,存储较小值的链表的下一个节点为其下一个节点与另一个链表头部的最小值存储节点。
判断链表是否有回环
思路:双指针,快慢两个指针遍历,如果两个指针重合,代表有回环;如果遍历到NULL,代表没有回环。
棋盘简单动态规划
棋子只允许向下或者向右行走,提问有几种到达方式:
1
| dp[i][j] = dp[i-1][j] + dp[i][j-1];
|
青蛙跳动态规划
思路:将石墩看成对岸,最后会简化为一个数据问题