栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

LeetCode链表:多指针和虚拟头结点

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode链表:多指针和虚拟头结点

链表操作的时间复杂度
操作时间复杂度
查找元素O(n)
插入元素O(1)
删除元素O(1)
链表的解题技巧
  • 多指针
    • 2个,快慢指针
    • 3个,链表反转
    • 4个,结点交换
  • 虚拟头结点
    • 常用于删除结点的情况
    • 其他情况也可具体分析
LeetCode 206
 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

 示例 1:
 输入:head = [1,2,3,4,5]
 输出:[5,4,3,2,1]
C++
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *pre = nullptr;
        ListNode *current = head;
        while (current != nullptr) {
            ListNode *next = current->next;
            
            // 反转
            current->next = pre;
            
            // 调整指针,准备进行下一次反转
            pre = current;
            current = next;
        }
        
        return pre;
    }
};
Swift
class Solution {
    func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
        let dummyHead = ListNode()
        dummyHead.next = head
        
        var pre: ListNode? = dummyHead
        var current: ListNode? = head
        
        while current != nil {
            let next = current?.next
            
            if current?.val == val {
                pre?.next = next    // 移除current
            } else {
                pre = current
            }
            
            current = next
        }
        
        return dummyHead.next
    }
}
LeetCode 203
 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

 示例 1:
 输入:head = [1,2,6,3,4,5,6], val = 6
 输出:[1,2,3,4,5]
C++
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode *dummyHead = new ListNode(0);
        dummyHead->next = head;
        
        ListNode *pre = dummyHead;
        ListNode *current = head;
        
        while (current != nullptr) {
            ListNode *next = current->next;
            
            if (current->val == val) {
                current->next = nullptr;
                pre->next = next;
                
                delete current;
            } else {
                pre = current;
            }
            
            current = next;
        }
        
        head = dummyHead->next;
        delete dummyHead;
        
        return head;
    }
};
Swift
class Solution {
    func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
        let dummyHead = ListNode()
        dummyHead.next = head
        
        var pre: ListNode? = dummyHead
        var current: ListNode? = head
        
        while current != nil {
            let next = current?.next
            
            if current?.val == val {
                pre?.next = next    // 移除current
            } else {
                pre = current
            }
            
            current = next
        }
        
        return dummyHead.next
    }
}

更多解题代码查看GitHub地址:https://github.com/Anzeron/Structure-Algorithm-LeetCode

阅读原文,关注公众号[空与一]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690436.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号