DSA: Linked List

For Interviews


Progress
Reviewed: 0%
8 Tasks


Linked lists are a popular topic in interviews, and understanding them thoroughly can be beneficial. They are frequently discussed in technical interviews, coding challenges, and assessments. Recruiters and interviewers often inquire about linked lists to assess a candidate's knowledge of data structures, algorithms, and problem-solving abilities. Mastering linked lists can help you tackle a wide range of programming problems efficiently.


  • Day 2
  • Singly Linked List Operations

    Concept:



    Resources:



    Assignments:


    Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
    The first node is considered odd, and the second node is even, and so on.
    Note that the relative order inside both the even and odd groups should remain as it was in the input.
    You must solve the problem in O(1) extra space complexity and O(n) time complexity.



  • Day 4
  • Circular Linked List

    Concept:



    Resources:



    Assignments:


    Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
    One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.

    Implement the MyCircularQueue class:

    MyCircularQueue(k) Initializes the object with the size of the queue to be k.
    int Front() Gets the front item from the queue. If the queue is empty, return -1.
    int Rear() Gets the last item from the queue. If the queue is empty, return -1.
    boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
    boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
    boolean isEmpty() Checks whether the circular queue is empty or not.
    boolean isFull() Checks whether the circular queue is full or not.

    You must solve the problem without using the built-in queue data structure in your programming language.



  • Day 5
  • Next Greater Node in Linked List

    Concept:



    Resources:



    Assignments:


    You are given the head of a linked list with n nodes.
    For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it.
    Return an integer array answer where answer[i] is the value of the next greater node of the ith node (1-indexed). If the ith node does not have a next greater node, set answer[i] = 0.



  • Day 6
  • Add Two Numbers

    Concept:



    Resources:



    Assignments:


    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

    You may assume the two numbers do not contain any leading zero, except the number 0 itself.

    Constraints:
    The number of nodes in each linked list is in the range [1, 100].
    0 <= Node.val <= 9
    It is guaranteed that the list represents a number that does not have leading zeros.

    Examples

    Example 1:

    Input: l1 = [2,4,3], l2 = [5,6,4]
    Output: [7,0,8]
    Explanation: 342 + 465 = 807.

    Example 2:
    Input: l1 = [0], l2 = [0]
    Output: [0]



  • Day 7
  • Swap Nodes in Pairs

    Concept:



    Resources:



    Assignments:


    Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

    Constraints:
    The number of nodes in the list is in the range [0, 100].
    0 <= Node.val <= 100

    Example 1:
    Input: head = [1,2,3,4]
    Output: [2,1,4,3]

    Example 2:
    Input: head = []
    Output: []

    Example 3:
    Input: head = [1]
    Output: [1]



  • Day 8
  • Partition List

    Concept:



    Resources:



    Assignments:


    Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

    You should preserve the original relative order of the nodes in each of the two partitions.

    Constraints:

    The number of nodes in the list is in the range [0, 200].
    -100 <= Node.val <= 100
    -200 <= x <= 200

    Example 1:

    Input: head = [1,4,3,2,5,2], x = 3
    Output: [1,2,2,4,3,5]

    Example 2:

    Input: head = [2,1], x = 2
    Output: [1,2]



  • Day 9
  • Middle of the Linked List

    Concept:



    Resources:



    Assignments:


    Given the head of a singly linked list, return the middle node of the linked list.
    If there are two middle nodes, return the second middle node.

    Constraints:

    The number of nodes in the list is in the range [1, 100].
    1 <= Node.val <= 100

    Example 1:
    Input: head = [1,2,3,4,5]
    Output: [3,4,5]
    Explanation: The middle node of the list is node 3.

    Example 2:
    Input: head = [1,2,3,4,5,6]
    Output: [4,5,6]
    Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.



  • Day 10
  • Remove Nth Node from end of list

    Concept:



    Resources:



    Assignments:


    Given the head of a linked list, remove the nth node from the end of the list and return its head.

    Constraints:

    The number of nodes in the list is sz.
    1 <= sz <= 30
    0 <= Node.val <= 100
    1 <= n <= sz

    Example 1:
    Input: head = [1,2,3,4,5], n = 2
    Output: [1,2,3,5]

    Example 2:
    Input: head = [1], n = 1
    Output: []

    Example 3:
    Input: head = [1,2], n = 1
    Output: [1]



  • Day 11
  • Delete the Middle Node of a Linked List

    Concept:



    Resources:



    Assignments:


    You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.

    The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.

    For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.

    Constraints:

    The number of nodes in the list is in the range [1, 10^5].
    1 <= Node.val <= 10^5

    Example 1:

    Input: head = [1,3,4,7,1,2,6]
    Output: [1,3,4,1,2,6]
    Explanation:
    Since n = 7, node 3 with value 7 is the middle node.
    We return the new list after removing this node.

    Example 2:

    Input: head = [1,2,3,4]
    Output: [1,2,4]
    Explanation:
    For n = 4, node 2 with value 3 is the middle node.

    Example 3:

    Input: head = [2,1]
    Output: [2]
    Explanation:
    For n = 2, node 1 with value 1 is the middle node.
    Node 0 with value 2 is the only node remaining after removing node 1.



  • Day 12
  • Linked List Cycle

    Concept:



    Resources:



    Assignments:


    Given head, the head of a linked list, determine if the linked list has a cycle in it.

    There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

    Return true if there is a cycle in the linked list. Otherwise, return false.

    Constraints:
    The number of the nodes in the list is in the range [0, 10^4].
    -10^5 <= Node.val <= 10^5
    pos is -1 or a valid index in the linked-list.

    Example 1:
    Input: head = [3,2,0,-4], pos = 1
    Output: true
    Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

    Example 2:
    Input: head = [1,2], pos = 0
    Output: true
    Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.

    Example 3:
    Input: head = [1], pos = -1
    Output: false
    Explanation: There is no cycle in the linked list.



  • Day 13
  • Design Browser History

    Concept:



    Resources:



    Assignments:


    Design a Browser History System


    You are to design a simplified browser history system with the ability to:

    • Visit a new URL
    • Go back a certain number of steps in history
    • Move forward a certain number of steps in history

    Class Definition


    Implement a class BrowserHistory with the following functionality:

    • BrowserHistory(string homepage)
      Initializes the browser history with the given homepage.
    • void visit(string url)
      Visits url from the current page and clears all forward history.
    • string back(int steps)
      Move back up to steps in history. Return the current URL after the move.
    • string forward(int steps)
      Move forward up to steps in history. Return the current URL after the move.

    Example Input & Output


    Input:
    ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
    [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
    
    Output:
    [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
      

    Explanation:

    • BrowserHistory browserHistory = new BrowserHistory("leetcode.com")
    • browserHistory.visit("google.com") → leetcode → google
    • browserHistory.visit("facebook.com") → leetcode → google → facebook
    • browserHistory.visit("youtube.com") → leetcode → google → facebook → youtube
    • browserHistory.back(1) → youtube → facebook → returns "facebook.com"
    • browserHistory.back(1) → facebook → google → returns "google.com"
    • browserHistory.forward(1) → google → facebook → returns "facebook.com"
    • browserHistory.visit("linkedin.com") → clears forward history → facebook → linkedin
    • browserHistory.forward(2) → cannot go forward → returns "linkedin.com"
    • browserHistory.back(2) → linkedin → facebook → google → returns "google.com"
    • browserHistory.back(7) → back to "leetcode.com" (start) → returns "leetcode.com"

    Constraints


    • 1 ≤ homepage.length, url.length ≤ 20
    • 1 ≤ steps ≤ 100
    • homepage and url consist of lowercase letters or '.'
    • At most 5000 operations will be made

    📝 Notes


    • Use a dynamic list or linked list to store the visited URLs.
    • Maintain a pointer to track the current page index.
    • Truncate the forward list when a new visit is made.



  • Day 14
  • Design Authentication Manager

    Concept:



    Resources:



    Assignments:


    There is an authentication system that works with authentication tokens. For each session, the user receives a token that expires timeToLive seconds after the currentTime.

    If a token is renewed, its expiry updates to be timeToLive seconds after the (possibly new) currentTime.


    You must implement the AuthenticationManager class with the following methods:

    • AuthenticationManager(int timeToLive) — Constructs the manager and sets the lifetime of tokens.
    • generate(string tokenId, int currentTime) — Generates a new token tokenId at currentTime.
    • renew(string tokenId, int currentTime) — Renews the token if it is unexpired. If already expired, it is ignored.
    • countUnexpiredTokens(int currentTime) — Returns the number of tokens that are still valid at currentTime.

    Note: If a token expires exactly at time t, and an action is called at time t, the expiration is considered to happen before the action.


    Example


    Input:

    ["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"]
    [[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]
      

    Output:

    [null, null, null, 1, null, null, null, 0]
      

    Explanation:

    AuthenticationManager authenticationManager = new AuthenticationManager(5);
    authenticationManager.renew("aaa", 1);      // No token "aaa" exists at time 1
    authenticationManager.generate("aaa", 2);   // Generate token "aaa" at time 2
    authenticationManager.countUnexpiredTokens(6); // "aaa" still valid → return 1
    authenticationManager.generate("bbb", 7);   // Generate token "bbb" at time 7
    authenticationManager.renew("aaa", 8);      // "aaa" expired at time 7 → ignored
    authenticationManager.renew("bbb", 10);     // Renew "bbb" to expire at 15
    authenticationManager.countUnexpiredTokens(15); // "bbb" expires at 15 → return 0
      

    Constraints

    • 1 ≤ timeToLive ≤ 10⁸
    • 1 ≤ currentTime ≤ 10⁸
    • 1 ≤ tokenId.length ≤ 5
    • tokenId contains only lowercase letters
    • Each generate call has a unique tokenId
    • currentTime values are strictly increasing
    • Maximum 2000 method calls total



×

Let's Go!

Congratulations on getting started. Here is a little reward for you...

×

10

Going to the next task in