Tutorialspoint
Problem
Solution
Submissions

Linked List with Basic Operations

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a JavaScript program to implement a singly linked list with basic operations including insertion, deletion, search, and display. The linked list should support operations like adding elements at the beginning, end, or at a specific position, and removing elements by value or position.

Example 1
  • Input: Insert 10, 20, 30 at end, then delete 20
  • Output: Initial: 10 -> 20 -> 30 -> null, After deletion: 10 -> 30 -> null
  • Explanation:
    • Create empty linked list.
    • Insert 10 at end: 10 -> null.
    • Insert 20 at end: 10 -> 20 -> null.
    • Insert 30 at end: 10 -> 20 -> 30 -> null.
    • Delete 20: 10 -> 30 -> null.
Example 2
  • Input: Insert 5 at beginning, insert 15 at position 1, search for 15
  • Output: List: 5 -> 15 -> null, Search result: Found at position 1
  • Explanation:
    • Create empty linked list.
    • Insert 5 at beginning: 5 -> null.
    • Insert 15 at position 1: 5 -> 15 -> null.
    • Search for 15 returns position 1 (0-indexed).
    • Display final list structure.
Constraints
  • 1 ≤ number of operations ≤ 100
  • -1000 ≤ node values ≤ 1000
  • Support insertion at beginning, end, and specific position
  • Support deletion by value and by position
  • Time Complexity: O(n) for most operations
  • Space Complexity: O(1) for operations, O(n) for storage
Linked ListHCL TechnologiesGoldman Sachs
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Create a Node class with data and next pointer
  • Implement LinkedList class with head pointer
  • Handle edge cases like empty list and single node operations
  • Use traversal for search and position-based operations
  • Update pointers carefully during insertion and deletion
  • Implement helper methods for common operations

Steps to solve by this approach:

 Step 1: Create a Node class with data field and next pointer to represent individual list elements.
 Step 2: Create LinkedList class with head pointer to track the first node.
 Step 3: Implement insertAtBeginning by creating new node and updating head pointer.
 Step 4: Implement insertAtEnd by traversing to the last node and linking new node.
 Step 5: Implement deleteByValue by finding the target node and updating pointer connections.
 Step 6: Implement search by traversing the list and returning position of target value.
 Step 7: Implement display method to print the entire list structure with proper formatting.

Submitted Code :