005 | 线性表面试经典中
程序员文章站
2022-06-09 15:53:54
...
写在前面:温故而知新,几天不练就要遗忘!
- 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则:
package link;
/**
* Create by IDEA
* User: zhangqi
* Date: 2019/3/9
* Desc: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则
*/
public class Solution {
/**
* 思路:建一个新链表,然后遍历比较两个链表并由小到大加入新的链表
* @param list1
* @param list2
* @return
*/
public ListNode Merge(ListNode list1,ListNode list2) {
/** 如果有一个链表为空,返回另一个 */
if(list1==null) return list2;
if(list2==null) return list1;
/** 建一个头结点并记录头结点的位置 */
ListNode list = new ListNode(-1);
ListNode head = list;
/** 遍历比较两个链表直至其中一个为空 */
while(list1!=null && list2!=null){
int val = list1.val;
if(val<list2.val){
list.next = list1;
list = list1;
list1 = list1.next;
}else{
list.next = list2;
list = list2;
list2 = list2.next;
}
}
/** 将遍历后不为空的链表设置为新链表的指针域 */
if(list1==null) list.next = list2;
if(list2==null) list.next = list1;
/**这里返回的是头结点的后继*/
return head.next;
}
/**
* 思路b:采用递归的方式,不过递归的方式稍有难度
* 重复性动作想到递归,递归想到栈,栈想到先进后出
* @param list1
* @param list2
* @return
*/
public ListNode Merge1(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;
/** 递归调用,先进后出 */
if (list1.val <= list2.val) {
list1.next = Merge1(list1.next, list2);
return list1;
} else {
list2.next = Merge1(list1, list2.next);
return list2;
}
}
- 输入两个链表,找出它们的第一个公共结点:
package link;
import java.util.HashMap;
/**
* Create by IDEA
* User: zhangqi
* Date: 2019/3/9
* Desc: 输入两个链表,找出它们的第一个公共结点
*/
public class Solution {
/**
* 思路a:一种简单的做法就是利用HashMap的特性
*
* @param pHead1
* @param pHead2
* @return
*/
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if (pHead1 == null || pHead2 == null) return null;
HashMap<ListNode, Integer> hashMap = new HashMap<>();
while (pHead1 != null) {
hashMap.put(pHead1, 1);
pHead1 = pHead1.next;
}
while (pHead2 != null) {
if (hashMap.containsKey(pHead2)) return pHead2;
pHead2 = pHead2.next;
}
return null;
}
/**
* 思路b:求出两个链表的长度,找出公共长度
* 然后两个链表在公共长度时同步遍历,判断是否有公共结点
* @param pHead1
* @param pHead2
* @return
*/
public ListNode FindFirstCommonNode1(ListNode pHead1, ListNode pHead2) {
if (pHead1 == null || pHead2 == null) return null;
/** 创建两个指针分别指向两个链表的头指针,作用是求出链表的长度 */
ListNode node1 = pHead1;
ListNode node2 = pHead2;
int len1 = 0;
int len2 = 0;
while (node1 != null) {
node1 = node1.next;
len1++;
}
while (node2 != null) {
node2 = node2.next;
len2++;
}
/** 找到公共长度,然后开始遍历判断是否存在公共结点 */
int len;
if (len1 >= len2) {
len = len1 - len2;
while (pHead1 != null) {
if (len > 0) {
pHead1 = pHead1.next;
len--;
} else {
if (pHead1 == pHead2) return pHead1;
pHead1 = pHead1.next;
pHead2 = pHead2.next;
}
}
} else {
len = len2 - len1;
if (len > 0) {
pHead2 = pHead2.next;
len--;
} else {
if (pHead1 == pHead2) return pHead1;
pHead1 = pHead1.next;
pHead2 = pHead2.next;
}
}
return null;
}
}
- 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
package link;
/**
* Create by IDEA
* User: zhangqi
* Date: 2019/3/9
* Desc: 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
* 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
*/
public class Solution {
/**
* 思路a:常规思路,遍历链表比较前后值,跳过重复值
*
* @param pHead
* @return
*/
public ListNode deleteDuplication(ListNode pHead) {
if (pHead == null) return null;
ListNode list = new ListNode(-1);
ListNode link = list;
while (pHead != null) {
int t = pHead.val;
if (pHead.next != null && t != pHead.next.val) {
list.next = pHead;
list = pHead;
pHead = pHead.next;
} else {
if (pHead.next == null) {
list.next = pHead;
list = pHead;
pHead = pHead.next;
}
while (pHead != null && t == pHead.val) {
pHead = pHead.next;
}
}
}
list.next = null;
return link.next;
}
/**
* 思路b:递归
* @param pHead
* @return
*/
public ListNode deleteDuplication1(ListNode pHead) {
if (pHead == null || pHead.next == null) return pHead;
if (pHead.val == pHead.next.val) {
/** 这里必须要新建一个引用标记当前结点 */
ListNode node = pHead.next;
while (node != null && pHead.val == node.val) {
node = node.next;
}
return deleteDuplication1(node);
} else {
pHead.next = deleteDuplication1(pHead.next);
return pHead;
}
}
}
推荐阅读
-
ThinkPHP中公共函数路径和配置项路径的映射分析_PHP
-
ASP.NET中的XML表单控件_MySQL
-
PHP中冒号、endif、endwhile、endfor这些都是什么,endwhileendfor_PHP教程
-
HTML 中 dl(dt,dd)、ul(li)、ol(li) 的使用方法
-
php中++i 与 i++ 的区别_php技巧
-
jQuery中关于live绑定多个事件整理的详解
-
phpstorm中js和php代码混用有错误提示,但是其实没问题,怎么关闭呢?
-
HTML5中5大存储方式汇总
-
JS中find(), findIndex(), filter(), forEach(), some(), every()方法记录
-
了不起的node.js读书笔记之node.js中的特性_node.js