Bird
Raised Fist0
DSA Javascriptprogramming~30 mins

Delete a Node from Circular Linked List in DSA Javascript - Build from Scratch

Choose your learning style10 modes available
Delete a Node from Circular Linked List
📖 Scenario: You are managing a playlist of songs that loops continuously. This playlist is represented as a circular linked list where each node contains a song name. You want to remove a specific song from this playlist.
🎯 Goal: Build a circular linked list with given songs, set a target song to delete, implement the deletion logic, and print the playlist after deletion.
📋 What You'll Learn
Create a circular linked list with nodes containing these songs in order: 'SongA', 'SongB', 'SongC', 'SongD'
Create a variable called targetSong with the value 'SongC'
Write a function called deleteNode that deletes the node containing targetSong from the circular linked list
Print the playlist after deletion in the format: SongA -> SongB -> SongD -> (back to SongA)
💡 Why This Matters
🌍 Real World
Circular linked lists are used in music players, round-robin schedulers, and games where the list loops continuously.
💼 Career
Understanding circular linked lists and node deletion is important for software engineers working with low-level data structures, embedded systems, or performance-critical applications.
Progress0 / 4 steps
1
Create the Circular Linked List
Create a class called Node with a constructor that takes data and sets next to null. Then create four nodes with data 'SongA', 'SongB', 'SongC', and 'SongD'. Link them in order and make the list circular by linking the last node back to the first node. Store the head node in a variable called head.
DSA Javascript
Hint

Remember to link the last node's next to the first node to make the list circular.

2
Set the Target Song to Delete
Create a variable called targetSong and set it to the string 'SongC'.
DSA Javascript
Hint

Just create a variable with the exact name and value.

3
Write the Delete Function
Write a function called deleteNode that takes head and targetSong as parameters. It should delete the node containing targetSong from the circular linked list and return the new head. Handle cases where the list is empty, the node to delete is the head, or the node is in the middle or end.
DSA Javascript
Hint

Remember to handle deleting the head node specially and keep the list circular.

4
Print the Playlist After Deletion
Call deleteNode with head and targetSong. Store the result in newHead. Then print the playlist starting from newHead in the format: SongA -> SongB -> SongD -> (back to SongA).
DSA Javascript
Hint

Use a loop to traverse the circular list until you reach the head again. Build a string with arrows and print it.

Practice

(1/5)
1. What happens when you delete the head node from a circular linked list with multiple nodes?
easy
A. The head moves to the next node and the list remains circular
B. The list becomes empty immediately
C. The last node points to null breaking the circle
D. The list throws an error and stops

Solution

  1. Step 1: Understand the head deletion in circular list

    When the head node is deleted, the next node becomes the new head.
  2. Step 2: Maintain circular link

    The last node's next pointer is updated to point to the new head, keeping the list circular.
  3. Final Answer:

    The head moves to the next node and the list remains circular -> Option A
  4. Quick Check:

    Head deletion updates head and last node pointer [OK]
Hint: Head deletion shifts head and keeps circle intact [OK]
Common Mistakes:
  • Assuming list becomes empty after head deletion
  • Breaking the circular link by setting last node to null
  • Not updating the last node's next pointer
2. Which of the following is the correct syntax to delete a node with value `key` from a circular linked list in JavaScript?
easy
A. function deleteNode(head) { /* code */ }
B. function deleteNode(key, head) { /* code */ }
C. function deleteNode(head, key) { /* code */ }
D. function deleteNode() { /* code */ }

Solution

  1. Step 1: Identify function parameters for deletion

    The function needs the head of the list and the key value to delete the correct node.
  2. Step 2: Confirm correct parameter order

    The common and clear order is (head, key) to access the list and the target value.
  3. Final Answer:

    function deleteNode(head, key) { /* code */ } -> Option C
  4. Quick Check:

    Parameters: head and key in order [OK]
Hint: Function needs head and key parameters in order [OK]
Common Mistakes:
  • Swapping parameter order causing confusion
  • Missing key parameter to identify node
  • Using no parameters making deletion impossible
3. What will be the output after deleting node with value 3 from this circular linked list: 1 -> 3 -> 5 -> 7 -> (back to 1)?
<pre>function deleteNode(head, key) { if (!head) return null; let curr = head, prev = null; do { if (curr.data === key) { if (prev) prev.next = curr.next; else { let temp = head; while (temp.next !== head) temp = temp.next; head = curr.next; temp.next = head; } break; } prev = curr; curr = curr.next; } while (curr !== head); return head; } // After deletion, print list starting from head
medium
A. 1 -> 5 -> 7 -> (back to 1)
B. 3 -> 5 -> 7 -> (back to 3)
C. 1 -> 3 -> 5 -> 7 -> (back to 1)
D. 5 -> 7 -> 1 -> (back to 5)

Solution

  1. Step 1: Locate node with value 3

    The code loops through nodes and finds node with data 3.
  2. Step 2: Delete node 3 and update links

    Since node 3 is not head, prev.next skips node 3 and points to node 5, maintaining circular link.
  3. Final Answer:

    1 -> 5 -> 7 -> (back to 1) -> Option A
  4. Quick Check:

    Node 3 removed, list links updated [OK]
Hint: Skip deleted node by linking previous to next [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting head incorrectly when key is not head
  • Breaking circular link by missing last node update
4. Identify the bug in this code snippet for deleting a node from a circular linked list:
function deleteNode(head, key) {
  if (!head) return null;
  let curr = head;
  while (curr.next !== head) {
    if (curr.next.data === key) {
      curr.next = curr.next.next;
      return head;
    }
    curr = curr.next;
  }
  return head;
}
medium
A. Infinite loop due to missing break
B. Does not handle deletion when head node has the key
C. Deletes multiple nodes with the key instead of one
D. Returns null even if list is not empty

Solution

  1. Step 1: Analyze loop and deletion logic

    The loop checks curr.next for key but never checks head node itself.
  2. Step 2: Identify missing head deletion case

    If head node contains key, this code does not delete it, causing incorrect behavior.
  3. Final Answer:

    Does not handle deletion when head node has the key -> Option B
  4. Quick Check:

    Head node deletion missing in logic [OK]
Hint: Always check head node separately for deletion [OK]
Common Mistakes:
  • Ignoring head node deletion case
  • Assuming loop covers all nodes including head
  • Not updating head pointer when head is deleted
5. Given a circular linked list with nodes 10 -> 20 -> 30 -> 40 -> (back to 10), how would you delete the last node (40) and maintain the circular structure? Choose the correct approach.
hard
A. Swap last node data with head and delete head
B. Set head to null directly to delete last node
C. Change last node's next to null to break the circle
D. Traverse to node before last, set its next to head, and delete last node

Solution

  1. Step 1: Find node before last node

    Traverse list until node.next.next equals head, identifying node before last.
  2. Step 2: Update next pointer and delete last node

    Set node before last's next pointer to head, removing last node and keeping list circular.
  3. Final Answer:

    Traverse to node before last, set its next to head, and delete last node -> Option D
  4. Quick Check:

    Update second last node's next to head [OK]
Hint: Link second last node directly to head [OK]
Common Mistakes:
  • Breaking circle by setting next to null
  • Deleting head instead of last node
  • Not updating pointers causing broken list
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard