链表基础算法题

本文详细介绍了链表的基础算法题目,包括反转链表、合并排序链表、删除链表节点、删除重复节点,以及快慢指针问题。特别讨论了如何找到链表的中间节点、环的入口节点、倒数第K个节点、判断回文链表以及相交链表的解法。同时,还涵盖了旋转右移链表和分隔链表的问题,提供了清晰的思路和解题方法。
摘要由CSDN通过智能技术生成

1.反转链表(重点)

反转链表: 输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

假设链表为 1→2→3→∅,反转后为 ∅←1←2←3。
在遍历链表时,将当前节点的next指针改为指向前一个节点。
由于节点没有引用其前一个节点,因此必须事先存储其前一个节点。
在更改引用之前,还需要存储后一个节点。最后返回新的头引用。

public ListNode reverseList(ListNode head) {
   
            ListNode pre=null;
            ListNode curr=head;
            while (curr!=null){
   
                ListNode next=curr.next;
                curr.next=pre;//让当前节点指向上一个节点
                pre=curr;//将当前节点保存到上一个当中
                curr=next;
            }
            return pre;
        }

2.合并两个排序的链表

NowCoder

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
   
        if(l1 == null && l2 == null) return null;
        ListNode head=new ListNode(-1);
        ListNode temp=head;
        while (l1!=null && l2 !=null){
   
            if(l1.val > l2.val){
   
                temp.next=l2;
                l2=l2.next;
            }else {
   
                temp.next=l1;
                l1=l1.next;
            }
            temp=temp.next;
        }
        temp.next= l1==null? l2:l1;
        return head.next;
    }

3.删除链表节点

https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/

    public ListNode deleteNode(ListNode head, int val) {
   
        if(head == null) return null;
        if(head.val==val) return head.next;
        //初始化双指针
        ListNode pre=head;
        ListNode cur=head.next;
        //根据题意绝对找得到 所以无需判断是否为空
        while (cur.val != val){
   
            pre=pre.next;
            cur=cur.next;
        }
        pre.next = cur.next;
        return head;
    }</
链表是一种常见的数据结构,常用于实现动态数组,它的每个节点包含一个值和指向下一个节点的指针。链表中的算法通常涉及到插入、删除、查找等操作。以下是一些常见的链表算法示例及其简单说明: 1. **单链表** - 搜索元素(时间复杂度O(n)): ```python class Node: def __init__(self, data=None): self.data = data self.next = None def search_list(head, target): current = head while current is not None: if current.data == target: return True current = current.next return False # 示例: head = Node(1) head.next = Node(2) head.next.next = Node(3) print(search_list(head, 2)) # 输出:True ``` 2. **反转链表** (递归或迭代法,时间复杂度O(n)或O(n/2)): ```python def reverse_list_recursively(head): if head is None or head.next is None: return head rest = reverse_list_recursively(head.next) head.next.next = head head.next = None return rest def reverse_list_iteratively(head): prev, curr = None, head while curr is not None: next_temp = curr.next curr.next = prev prev = curr curr = next_temp return prev # 示例: head = Node(1, Node(2, Node(3))) reversed_head = reverse_list_iteratively(head) ``` 3. **合并两个有序链表** (时间复杂度O(m+n),其中m和n分别是两个链表的长度): ```python def merge_sorted_lists(l1, l2): dummy = Node(0) # 创建虚拟头节点 current = dummy while l1 and l2: if l1.data <= l2.data: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next current.next = l1 if l1 is not None else l2 return dummy.next # 示例: l1 = Node(1, Node(3)) l2 = Node(2, Node(4)) merged_head = merge_sorted_lists(l1, l2) ``` 这些例子展示了链表的一些基础操作,实际的运行结果取决于链表的具体状态。如果你有特定的问,比如遇到了某个算法实现上的困难,或者想了解其他类型的链表,请告诉我具体的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值