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

相关推荐
阿猿收手吧!2 分钟前
【C++】string_view:高效字符串处理指南
开发语言·c++
风生u28 分钟前
activiti7 详解
java
玄同76532 分钟前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
岁岁种桃花儿37 分钟前
SpringCloud从入门到上天:Nacos做微服务注册中心(二)
java·spring cloud·微服务
Word码40 分钟前
[C++语法] 继承 (用法详解)
java·jvm·c++
Yorlen_Zhang43 分钟前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
lxl130744 分钟前
C++算法(1)双指针
开发语言·c++
TT哇1 小时前
【实习 】银行经理端两个核心功能的开发与修复(银行经理绑定逻辑修复和线下领取扫码功能开发)
java·vue.js
逝水如流年轻往返染尘1 小时前
Java中的数组
java
不绝1911 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#