• #3 - Linked Lists - Data Structures and Algorithms using C++ | Akash Vishwakarma

    #3 - Linked Lists - Data Structures and Algorithms using C++ | Akash Vishwakarma

    2 months ago 6.36k views
    Hi its me Akash Vishwakarma, This is Article No #3 of Our DSA Series on Skytup. In this Article We will learn about Linked Lists. This article is written with the help of Ai Tools and some of my own expertise 🙏.

    Introduction to Linked Lists

    A linked list is a fundamental data structure that consists of a sequence of elements, where each element points to the next element in the sequence. Unlike arrays, linked lists don't store elements in contiguous memory locations, making them more flexible for dynamic data management.

    Key Advantages of Linked Lists:

    • Dynamic size - can grow or shrink during runtime
    • Efficient insertion and deletion at the beginning
    • Flexible memory allocation
    • No memory wastage as size can be adjusted

    Types of Linked Lists

    1. Singly Linked List

    A singly linked list is the simplest type of linked list where each node contains data and a pointer to the next node in the sequence. The last node points to NULL.

    
    class Node {
        int data;
        Node next;
        
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    2. Doubly Linked List

    In a doubly linked list, each node contains data and two pointers - one pointing to the next node and another pointing to the previous node. This allows for bidirectional traversal.

    
    class DoublyNode {
        int data;
        DoublyNode next;
        DoublyNode prev;
        
        DoublyNode(int data) {
            this.data = data;
            this.next = null;
            this.prev = null;
        }
    }

    3. Circular Linked List

    A circular linked list is a variation where the last node points back to the first node, creating a circle. It can be either singly or doubly circular.

    Essential Operations

    Reversing a Linked List

    Reversing a linked list is a fundamental operation that can be performed either iteratively or recursively.

    
    Node reverseList(Node head) {
        Node prev = null;
        Node current = head;
        Node next = null;
        
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        return prev;
    }

    Floyd's Cycle Detection Algorithm

    Also known as the "tortoise and hare" algorithm, this method detects cycles in a linked list using two pointers moving at different speeds.

    
    boolean hasCycle(Node head) {
        if (head == null) return false;
        
        Node slow = head;
        Node fast = head;
        
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) return true;
        }
        return false;
    }

    Merging Sorted Lists

    Merging two sorted linked lists while maintaining the sort order is a common operation.

    
    Node mergeSortedLists(Node l1, Node l2) {
        Node dummy = new Node(0);
        Node current = dummy;
        
        while (l1 != null && l2 != null) {
            if (l1.data <= l2.data) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }
            current = current.next;
        }
        
        if (l1 != null) current.next = l1;
        if (l2 != null) current.next = l2;
        
        return dummy.next;
    }

    Common Interview Questions

    1. Find the Middle Node

    Question: How would you find the middle node of a linked list in one pass?

    Answer: Use the slow and fast pointer technique. Move one pointer at normal speed and another at double speed. When the fast pointer reaches the end, the slow pointer will be at the middle.

    2. Detect and Remove Cycle

    Question: How would you detect and remove a cycle in a linked list?

    Answer: First use Floyd's algorithm to detect the cycle. Then place one pointer at head and another at the meeting point. Move both one step at a time until they meet at the cycle's start. Remove the cycle by setting the next pointer to null.

    3. Reverse in Groups

    Question: How would you reverse a linked list in groups of k?

    Answer: Use a recursive approach where you reverse k nodes at a time, then recursively call the function for the remaining nodes.

    4. Intersection Point

    Question: How would you find the intersection point of two linked lists?

    Answer: Calculate the length difference of both lists. Move the pointer of the longer list ahead by the difference. Then move both pointers together until they meet at the intersection point.

    Time Complexity Analysis

    Operation Singly Linked List Doubly Linked List
    Insertion at beginning O(1) O(1)
    Insertion at end O(n) O(1)
    Deletion at beginning O(1) O(1)
    Deletion at end O(n) O(1)
    Search O(n) O(n)

    Keywords

    History of Programming Languages ⚡

    History of Programming Languages ⚡

    1 year ago 15.47k views

    Embark on a captivating journey through the history of programming, from its humble beginnings in the 19th century to the cutting-edge innovations of the 21st century. Discover the key milestones, gro

    Understanding MERN Stack: The Easiest Full-Stack Development Tools

    Understanding MERN Stack: The Easiest Full-Stack Development Tools

    11 months ago 11.26k views

    Discover the MERN stack, a popular full-stack development framework combining MongoDB, Express.js, React.js, and Node.js. Learn why it&#039;s considered one of the easiest and most efficient tools

    Big O Notation: asymptotic analysis (big-o notation)

    Big O Notation: asymptotic analysis (big-o notation)

    10 months ago 6.75k views

    This article will cover the basics of Big O notation, explaining its importance in algorithm analysis. It will break down common complexities and provide examples to demonstrate how Big O affects perf

    Top Linux Commands You Must Know As Linux User ?

    Top Linux Commands You Must Know As Linux User ?

    10 months ago 7.46k views

    Linux is a powerful and versatile operating system widely used in various computing environments, from servers and workstations to embedded systems and supercomputers. At the heart of Linux lies its c

    Top Git Commands you must know | Git and Github

    Top Git Commands you must know | Git and Github

    10 months ago 12.33k views

    Git is an essential tool for modern software development, enabling developers to manage and collaborate on code efficiently. In this comprehensive guide, we'll explore the top Git commands and workflo

    React.js Essentials: A Beginner&#039;s Journey

    React.js Essentials: A Beginner's Journey

    10 months ago 17.25k views

    React.js is a revolutionary JavaScript library for building dynamic user interfaces. This comprehensive guide takes you on a beginner's journey to master React.js essentials. Explore the core concepts

    Setting Up a Linux Dedicated Server for Web Development

    Setting Up a Linux Dedicated Server for Web Development

    10 months ago 14.3k views

    This article provides a comprehensive guide on setting up a Linux-based dedicated server for hosting PHP websites and Node.js websites. It is divided into two sections, one for PHP website hosting and

    Configuring a Linux Server for Python and Ruby on Rails Development

    Configuring a Linux Server for Python and Ruby on Rails Development

    10 months ago 15.72k views

    This comprehensive article guides you through the process of configuring a Linux server for web development using Python and Ruby on Rails. It is divided into three main sections: Python Web Developme

    Cloud platforms : Aws, Google Cloud and Microsoft Azure

    Cloud platforms : Aws, Google Cloud and Microsoft Azure

    10 months ago 9.24k views

    Cloud platforms offer businesses unprecedented scalability, flexibility, and cost-efficiency. This beginner's guide demystifies the concept, exploring the key players – Amazon Web Services, Mic

    Angular js : Let&#039;s learn something about Angular js

    Angular js : Let's learn something about Angular js

    10 months ago 7.33k views

    AngularJS, created by Misko Hevery and Adam Abrons in 2009, is a powerful JavaScript framework for building dynamic web applications. This guide covers its creation, syntax, and key features, such as

    Flutter: A cross platform development technology.

    Flutter: A cross platform development technology.

    10 months ago 6.46k views

    Flutter is an open-source UI software development toolkit created by Google. It is used to develop cross-platform applications for Android, iOS, Linux, macOS, Windows, Google Fuchsia, and the web from

    Login Interface using Flutter - Cross Platform

    Login Interface using Flutter - Cross Platform

    10 months ago 10.58k views

    Flutter is a powerful framework for building cross-platform mobile applications. It provides a wide range of widgets and tools that make UI development straightforward and efficient. In this article,

    How to Cast Your Phone Screen and sound to PC without Using any third party app?

    How to Cast Your Phone Screen and sound to PC without Using any third party app?

    9 months ago 14.5k views

    Mirroring your Android phone screen to a PC with ADB is simple. Enable USB debugging, install ADB on your PC, connect your phone, verify the connection with adb devices, and use adb platform tools to

    What is debouncing and why it is important?

    What is debouncing and why it is important?

    9 months ago 11.01k views

    Discover the concept of debouncing in electronics and programming. Learn how to eliminate false signals with hardware and software techniques, including advanced methods like interrupt-based debouncin

    Keywords

    programming(24) tech(20) coding(9) cpp(9) dsa(7) python(6) javascript(5) placements(5) web development(4) problem-solving(4)