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?

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);
    }
}

或者手动反转:

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判圈算法)。

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的中间节点?

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

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?

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

  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ArrayList和LinkedList都是List接口的两种不同的实现,它们在内部数据结构和操作性能上有一些区别。ArrayList底层使用数组来存储元素,而LinkedList则使用双向链表来存储元素。这些区别导致了它们在不同场景下的适用性不同。 1. 内存占用: - ArrayList在内存中占用的空间相对较大,因为它需要预留一定大小的数组空间。 - LinkedList在内存中占用的空间相对较小,因为它只需要存储节点和指针。 2. 插入和删除操作: - ArrayList在末尾插入和删除元素的性能较好,时间复杂度为O(1)。但在中间插入和删除元素时,需要将后续元素依次向后或向前移动,时间复杂度为O(n)。 - LinkedList在任意位置插入和删除元素的性能都较好,时间复杂度为O(1),因为它只需要修改相邻节点的指针即可。 3. 随机访问: - ArrayList可以通过索引直接访问元素,时间复杂度为O(1)。 - LinkedList需要从头节点或尾节点开始遍历,直到找到目标节点,时间复杂度为O(n)。 综上所述,当需要频繁进行随机访问操作时,应该使用ArrayList;而在频繁进行插入和删除操作时,LinkedList更加高效。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [经典面试2(ArrayList 和 LinkedList )](https://blog.csdn.net/qq_52485934/article/details/129829535)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [LinkedList相关面试](https://blog.csdn.net/zhangjin1120/article/details/119056245)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值