java数据结构OJ练习(二)
目录
一、删除重复节点
【题目】在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,
重复的结点不保留,返回链表头指针。 例如,链表 1->2->3->3->4->4->5 处理
后为 1->2->5
【分析】首先,遍历链表。查找重复节点;又因为链表是有序链表,则重复节点是相邻的。若是重复节点,则需要循环遍历查找,直到查找到不是重复的链表,将其尾插到新链表中;否则,直接将该节点插入到新节点中。
class ListNode{
int val;
ListNode next;
public ListNode(int v){
val = v;
}
}
public class LinkedList {
//在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,
// 重复的结点不保留,返回链表头指针。
public ListNode deleteDuplication(ListNode pHead) {
//先考虑特殊极端情况
if(pHead == null){
return null;
}
if(pHead.next == null){
//链表中只有一个节点
return pHead;
}
//创建一个带傀儡节点的新链表用来存储结果
ListNode newHead = new ListNode(0);
//为了尾插方便,创建一个引用标记尾部节点
ListNode newTail = newHead;
//处理一般情况,遍历链表,判断其是否存在重复节点
ListNode cur = pHead;
//解引用的时候需要判断引用是否为空
while (cur != null ){
if(cur.next != null && cur.val == cur.next.val){
//cur是重复节点,继续找到不重复的节点位置,查找中间出现多个重复节点的情况
while (cur != null && cur.next != null &&
cur.val == cur.next.val){
cur = cur.next;
}
//循环到此处,要么是cur已到达末尾,要么是cur找到了不重复的节点
//此时需要更新cur指向下一个不重复的节点
cur = cur.next;
}else{
//cur不是重复节点,直接插入到 newHead 的末尾即可
newTail.next = new ListNode(cur.val);
newTail = newTail.next;
}
cur = cur.next;
}
return newHead.next;
}
}
二、回文单链表判断
【题目】对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,
判断其是否为回文结构。
【分析】首先,单链表无法前后双向遍历,只能单向遍历,因此,可以将原链
表逆置成为一个新的单链表,再和原链表一一对比看其是否构成回文结构。
- 方法1:空间复杂度为O(N)
public boolean chkPalindrome1(ListNode A) {
if(A == null){
return true;
}
if (A.next == null){
return true;
}
//思路:1、先把原链表复制一份
ListNode newHead = new ListNode(0);
ListNode newTail = newHead;
for (ListNode cur = A;cur!= null;cur = cur.next){
newTail.next = new ListNode(cur.val);
newTail = newTail.next;
}
ListNode B = newHead.next;
//2、对新链表逆置
ListNode prev = null;
ListNode cur = B;
while (cur != null){
ListNode next = cur.next;
if (next == null){
//cur指向了最后一个节点,也就是逆置后链表的头结点
B = cur;
}
//逆置的核心操作:掰道岔
cur.next = prev;
//更新循环变量
prev = cur;
cur = next;
}
//3、对比新旧链表
ListNode cur1 = A;
ListNode cur2 = B;
while (cur1 != null && cur2 != null){
if(cur1.val != cur2.val){
//找到了反例,不是回文结构
return false;
}
cur1 = cur1.next;
cur2 = cur2.next;
}
return true;
}
-
方法二:空间复杂度为O(1);
思路:将链表的后半部分逆置,再对比前后两部分是否构成回文结构
public boolean chkPalindrome2(ListNode A) {
if (A == null) {
return true;
}
if (A.next == null) {
return true;
}
//思路
//1、找到中间节点
int length = getLength(A);
int steps = length / 2;
ListNode B = A;
for (int i = 0;i < steps;i++){
B = B.next;
} //此时的B指向了中间节点
//2、对后半部分链表进行逆置
ListNode prev = null;
ListNode cur = B;
while (cur != null){
ListNode next = cur.next;
if (next == null){
B = cur;
}
//掰道岔
cur.next = prev;
//更新循环变量
prev = cur;
cur = next;
}
//3、对比两半个链表是否一样
ListNode cur1 = A;
ListNode cur2 = B;
while (cur1 != null && cur2 != null){
if (cur1.val != cur2.val){
return false;
}
cur1 = cur1.next;
cur2 = cur2.next;
}
return true;
}
三、相交链表判断
【题目】给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个
单链表相交的起始节点。如果两个链表不存在相交节点,返回 null
【分析】对于两个长度不相同的链表,先设置引用使其同步,在判断是否“相遇”。
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//step1:先求两个链表的长度
int length1 = getLength(headA);
int length2 = getLength(headB);
//step2:判断两个链表的长度
if(length1 > length2){
int steps = length1 - length2;
for (int i = 0; i < steps;i++){
headA = headA.next;
}
}else {
int steps = length2 - length1;
for (int i = 0;i < steps;i++){
headB = headB.next;
}
}
//step3:A 和 B 已经同步,接下来需要判断是否“相遇”
while (headA != null && headB != null){
if (headA == headB){
//A 和 B 相遇了
return headA;
}
headA = headA.next;
headB = headB.next;
}
return null; //没找到交点
}
public int getLength(ListNode head){
int length = 0;
for (ListNode cur = head;cur != null;cur = cur.next){
length++;
}
return length;
}
四、判断链表是否带环
【题目】给你一个链表的头节点 head ,判断链表中是否有环
【分析】利用“快慢指针”,代环链表中,快指针在循环过程中一定会和慢指针重合。
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if (fast == slow){
return true;
}
}
return false;
}
五、寻找代环链表的入口
【题目】给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
【分析】利用“快慢指针”,代环链表中,快指针在循环过程中一定会和慢指针重合。
public class LinkedList {
//寻找啊代环链表的入口
public ListNode detectCycle(ListNode head) {
//思路:利用快慢指针
//step1:先找到快慢指针重合的位置
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if (fast == slow){
break;
}
}
if (fast == null || fast.next == null){
return null;
//此时的链表不带环
}
//对于代环链表,从fast 和 slow 交汇位置以及链表头位置同时出发
//相遇的位置就是环入口位置
ListNode cur1 = head;
ListNode cur2 = slow;
while (cur1 != cur2){
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
//虽然解引用链表要判断非空,但是此时程序以及进入了代环链表,
// 带环链表是非空的,故此处不需要判断链表是否非空
}