LinkedList常考面试题

LinkedList是Java集合框架中的一个重要部分,它是一种线性数据结构,不同于ArrayList基于数组实现,LinkedList是基于双向链表实现的。这使得它在插入、删除操作上具有较高的效率,但随机访问元素时效率较低。以下是一些关于LinkedList的常考面试题及其答案,包括代码示例。

1. LinkedList与ArrayList的区别?

  • 数据结构:ArrayList是基于动态数组实现的,而LinkedList是基于双向链表实现的。
  • 随机访问:ArrayList支持快速随机访问,时间复杂度为O(1);而LinkedList需要遍历链表,时间复杂度为O(n)。
  • 插入和删除:在列表的开始或中间插入、删除元素时,LinkedList更高效,时间复杂度为O(1);ArrayList在这些操作上需要移动元素,时间复杂度为O(n)。
  • 空间开销:LinkedList每个节点除了存储元素外,还需要额外的空间存储前后节点的引用,因此空间开销相对较大。

2. 如何反转一个LinkedList?

java 复制代码
import java.util.Collections;
import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println("Original List: " + list);
        
        Collections.reverse(list);
        System.out.println("Reversed List: " + list);
    }
}

或者手动反转:

java 复制代码
public class LinkedListExample {
    // Node class for LinkedList
    static class Node {
        int data;
        Node prev, next;
        
        Node(int d) {
            data = d;
            prev = next = null;
        }
    }
    
    static Node reverse(Node head) {
        Node prev = null;
        Node current = head;
        Node next = null;
        
        while (current != null) {
            next = current.next;
            current.next = prev;
            current.prev = next;
            prev = current;
            current = next;
        }
        return prev;
    }
    
    // ... (其余代码省略,包括打印链表等)
}

3. 如何检测LinkedList中是否有环?

可以使用快慢指针法(Floyd判圈算法)。

java 复制代码
public boolean hasCycle(ListNode head) {
    if (head == null || head.next == null) {
        return false;
    }
    ListNode slow = head;
    ListNode fast = head.next;
    while (fast != null && fast.next != null) {
        if (slow == fast) {
            return true;
        }
        slow = slow.next;
        fast = fast.next.next;
    }
    return false;
}

4. 如何找到LinkedList的中间节点?

同样可以使用快慢指针法。

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

5. 如何合并两个排序的LinkedList?

java 复制代码
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    ListNode tail = dummy;
    
    while (l1 != null && l2 != null) {
        if (l1.val < l2.val) {
            tail.next = l1;
            l1 = l1.next;
        } else {
            tail.next = l2;
            l2 = l2.next;
        }
        tail = tail.next;
    }
    
    if (l1 != null) {
        tail.next = l1;
    } else if (l2 != null) {
        tail.next = l2;
    }
    
    return dummy.next;
}

6. LinkedList的线程安全性问题。

LinkedList不是线程安全的。在多线程环境中,多个线程同时修改LinkedList可能会导致数据不一致或其他并发问题。因此,在使用LinkedList时,需要额外的同步措施来确保线程安全,如使用Collections.synchronizedList()方法或ConcurrentLinkedQueue等并发集合类。

7. 补充ing

相关推荐
nightunderblackcat5 分钟前
进阶向:人物关系三元组,解锁人物关系网络的钥匙
开发语言·python·开源·php
MrSYJ23 分钟前
UserDetailService是在什么环节生效的,为什么自定义之后就能被识别
java·spring boot·后端
MrSkye24 分钟前
🔥React 新手必看!useRef 竟然不能触发 onChange?原来是这个原因!
前端·react.js·面试
科大饭桶1 小时前
C++入门自学Day11-- String, Vector, List 复习
c语言·开发语言·数据结构·c++·容器
范范之交1 小时前
JavaScript基础语法two
开发语言·前端·javascript
Felven1 小时前
C. Game of Mathletes
c语言·开发语言
long3162 小时前
构建者设计模式 Builder
java·后端·学习·设计模式
吐个泡泡v2 小时前
Maven 核心命令详解:compile、exec:java、package 与 IDE Reload 机制深度解析
java·ide·maven·mvn compile
点云SLAM2 小时前
C++中内存池(Memory Pool)详解和完整示例
开发语言·c++·内存管理·内存池·new/delete·malloc/free
天上掉下来个程小白2 小时前
微服务-01.导入黑马商城
java·微服务·架构