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

相关推荐
渣哥16 分钟前
原来 Java 里线程安全集合有这么多种
java
间彧23 分钟前
Spring Boot集成Spring Security完整指南
java
间彧1 小时前
Spring Secutiy基本原理及工作流程
java
前端付豪1 小时前
1、震惊!99% 前端都没搞懂的 JavaScript 类型细节
前端·javascript·面试
Java水解2 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆4 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学4 小时前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole4 小时前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端
华仔啊5 小时前
基于 RuoYi-Vue 轻松实现单用户登录功能,亲测有效
java·vue.js·后端