算法通关村第一关|黄金挑战|链表中的环问题&双向链表

1.链表中环的问题

1.1 判断是否有环:使用集合处理,判断是否碰撞。

java 复制代码
public ListNode detectCycle(ListNode head) {
	ListNode pos = head;
	Set<ListNode> visited = new HashSet<ListNode>();
	while (pos != null) {
        if (visited.contains(pos)) {
            return pos;
        } else {
            visited.add(pos);
        }
        pos = pos.next;
    }
	return null;
}

1.2 判断是否有环:使用快慢指针,如果有环,慢的就一定能遇到快的。

java 复制代码
public boolean hasCycle(ListNode head) {
	if (head == null || head.next == null) {
        return false;
	}
	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;
}

1.3 有环时判断环的位置:使用快慢指针。

java 复制代码
public ListNode detectCycle(ListNode head) {
	if (head == null) {
        return null;
	}
	ListNode slow = head, fast = head;
	while (fast != null) {
        slow = slow.next;
        if (fast.next != null) {
            fast = fast.next.next;
        } else {
            return null;
        }
        if (fast == slow) {
            ListNode ptr = head;
            while (ptr != slow) {
                ptr = ptr.next;
                slow = slow.next;
            }
            return ptr;
        }
    }
	return null;
}

2.双向链表

2.1 双向链表的结点。

java 复制代码
class DoubleNode {
    public int data;
    public DoubleNode next;
    public DoubleNode prev;
    public DoubleNode(int data) {
        this.data = data;
    }

    // 打印结点数据域
    public void displayNode() {
        System.out.println("{" + data + "}");
    }
}

2.2 双向链表的结构和遍历方法。

java 复制代码
public class DoublyLinkList {
    // 分别指向双向链表的头部和尾部
    private DoubleNode first;
    private DoubleNode last;
    public DoublyLinkList() {
        first = null;
        last = first;
    }
    // 从头部开始打印
    public void displayForward() {
        System.out.println("List(first--->last):");
        DoubleNode current = first;
        while (current != null) {
            current.displayNode();
            current = current.next;
        }
        System.out.println();
    }
    // 从尾部开始演绎
    public void displayBackward() {
        System.out.println("List(last--->first):");
        DoubleNode current = last;
        while (current != null) {
            current.displayNode();
            current = current.prev;
        }
        System.out.println();
    }
}

2.3 双向链表插入元素:在头部和尾部插入元素。

java 复制代码
// 头部插入
public void insertFirst(int data) {
	DoubleNode newDoubleNode = new DoubleNode(data);
	if (first == null) {
        last = newDoubleNode;
    } else {
        first.prev = newDoubleNode;
    }
	newDoubleNode.next = first;
	first = newDoubleNode;
}
//尾部插入
public void insertLast(int data) {
	DoubleNode newDoubleNode = new DoubleNode(data);
    if (first == null) {
        first = newDoubleNode;
    } else {
        newDoubleNode.prev = last;
        last.next = newDoubleNode;
    }
    last = newDoubleNode;
}

2.4 双向链表插入元素:在中间插入元素。

java 复制代码
public void insertAfter(int key, int data) {
	DoubleNode newDoubleNode = new DoubleNode(data);
    DoubleNode current = first;
    while ((current != null) && (current.data != key)) {
        current = current.next;
    }
    // 当前结点current为空
    if (current == null) {
        if (first == null) {
            // 链表中没有元素,直接赋值
            first = newDoubleNode;
            last = newDoubleNode;
        } else {
            // 找不到key,在链表尾部插入新结点
            last.next = newDoubleNode;
            newDoubleNode.prev = last;
            last = newDoubleNode;
        }
    } else {// 链表中有元素,并且找到了key
        if (current == last) {
            // key与最后结点的data相等
            newDoubleNode.next = null;
            last = newDoubleNode;
        } else {
            // 两结点中间插入
            newDoubleNode.next = current.next;
            current.next.prev = newDoubleNode;
        }
        current.next = newDoubleNode;
        newDoubleNode.prev = current;
    }
}

2.5 双向链表删除元素:删除首尾元素。

java 复制代码
// 删除首部元素
public DoubleNode deleteFirst() {
	DoubleNode temp = first;
    if (first.next = null) {
    	last = null;
    } else {
        first.next.prev = null;
    }
    first = first.next;
    return temp;
}

// 删除尾部元素
public DoubleNode deleteLast() {
	DoubleNode temp = last;
    if (first.next == null) {
        first = null;
    } else {
        last.prev.next = null;
    }
    last = last.prev;
    return temp;
}

2.6 双向链表删除元素:删除中间元素。

java 复制代码
public DoubleNode deleteKey(int key) {
	DoubleNode current = first;
	while (current != null && current.data != key) {
        current = current.next;
    }
	if (current == null) {
        return null;
    } else {
        if (current == first) {
            first = current.next;
            current.next.prev = null;
        } else if (current == last) {
            last = current.prev;
            current.prev.next = null;
        } else {
            current.prev.next = current.next;
            current.next.prev = current.prev;
        }
    }
	return current;
}

如果对您有帮助,请点赞关注支持我,谢谢!❤

如有错误或者不足之处,敬请指正!❤

相关推荐
坚持编程的菜鸟7 小时前
模拟实现memmove
c语言·算法·模拟实现memmove
码农颜8 小时前
5.4.1 锁分类
java·数据库·mysql
wabs6669 小时前
关于图论【最短路径之Dijkstra算法(堆优化版)|卡码网47.参加科学大会的思考】
数据结构·算法·图论·优先级队列·邻接表·小顶堆·卡码网
坚持编程的菜鸟9 小时前
编写判断大小端程序
c语言·算法·判断大小端
papaofdoudou9 小时前
判断排列逆序奇偶性的乘积判别法(范德蒙德符号法)
人工智能·算法
linux-hzh10 小时前
百日算法修炼 · Day 03
java·算法
问商十三载10 小时前
RAG 检索效果差怎么排查?2026 五层诊断法完整指南
开发语言·人工智能·windows·python·算法
不负岁月无痕10 小时前
简单理解操作系统结构
java·linux·c语言·开发语言·c++·面试
mister_guo10 小时前
Java线程池参数应该如何设置(ThreadPoolExecutor)
java
余额瞒着我当琳10 小时前
C++模板初阶与STL初探
android·java·c++