Given the head of a singly linked list and an integer k, delete the kth node of the linked list and return the head of the modified list.
Input: head -> 3 -> 4 -> 5, k = 2
Output: head -> 3 -> 5
Explanation: The 2nd node with value 4 was removed.
Input: head -> 1 -> 2 -> 3, k = 1
Output: head -> 2 -> 3
Explanation: The 1st Node was removed, note that the value of the head has changed.
Input: head -> 7 -> 7 -> 7, k = 3
NULL
.NULL
:
null
, as there is nothing to delete.null
.#include <bits/stdc++.h>
using namespace std;
// Node structure
struct ListNode {
int val;
ListNode *next;
ListNode(): val(0), next(nullptr) {}
ListNode(int data1): val(data1), next(nullptr) {}
ListNode(int data1, ListNode *next1): val(data1), next(next1) {}
};
class Solution {
public:
// Function to delete the k-th node of a linked list
ListNode* deleteKthNode(ListNode* head, int k) {
// If the list is empty, return NULL
if (head == NULL)
return NULL;
// If k is 1, delete the head node
if (k == 1) {
ListNode* temp = head;
head = head->next;
delete temp;
return head;
}
// Initialize a temporary pointer
ListNode* temp = head;
// Traverse to the (k-1)th node
for (int i = 0; temp != NULL && i < k - 2; i++) {
temp = temp->next;
}
/*If k is greater than the number of nodes,
return the unchanged list*/
if (temp == NULL || temp->next == NULL)
return head;
// Delete the k-th node
ListNode* next = temp->next->next;
delete temp->next;
temp->next = next;
// Return head
return head;
}
};
// Function to print the linked list
void printLL(ListNode* head) {
ListNode* current = head;
while (current != nullptr) {
cout << current->val << " ";
current = current->next;
}
cout << endl;
}
int main() {
// Initialize a vector with values for the linked list
vector<int> arr = {12, 5, 8, 7};
// Create a linked list with the values from the vector
ListNode* head = new ListNode(arr[0]);
head->next = new ListNode(arr[1]);
head->next->next = new ListNode(arr[2]);
head->next->next->next = new ListNode(arr[3]);
// Print the original linked list
cout << "Original list: ";
printLL(head);
// Creating an instance of Solution class
Solution sol;
// Call the deleteKthNode function to delete the k-th node
int k = 2;
head = sol.deleteKthNode(head, k);
// Print the linked list after deletion
cout << "List after deleting the kth node: ";
printLL(head);
return 0;
}
class ListNode {
int val;
ListNode next;
// Constructor
ListNode() {
this.val = 0;
this.next = null;
}
ListNode(int data1) {
this.val = data1;
this.next = null;
}
ListNode(int data1, ListNode next1) {
this.val = data1;
this.next = next1;
}
}
class Solution {
// Function to delete the k-th node of a linked list
public ListNode deleteKthNode(ListNode head, int k) {
// If the list is empty, return null
if (head == null)
return null;
// If k is 1, delete the head node
if (k == 1) {
ListNode temp = head;
head = head.next;
return head;
}
// Initialize a temporary pointer
ListNode temp = head;
// Traverse to the (k-1)th node
for (int i = 0; temp != null && i < k - 2; i++) {
temp = temp.next;
}
/* If k is greater than the number of nodes,
return the unchanged list */
if (temp == null || temp.next == null)
return head;
// Delete the k-th node
ListNode next = temp.next.next;
temp.next = next;
// Return head
return head;
}
}
class Main {
// Function to print the linked list
private static void printLL(ListNode head) {
ListNode current = head;
while (current != null) {
System.out.print(current.val + " ");
current = current.next;
}
System.out.println();
}
// Main method
public static void main(String[] args) {
// Initialize an array with values for the linked list
int[] arr = {12, 5, 8, 7};
// Create a linked list with the values from the array
ListNode head = new ListNode(arr[0]);
head.next = new ListNode(arr[1]);
head.next.next = new ListNode(arr[2]);
head.next.next.next = new ListNode(arr[3]);
// Print the original linked list
System.out.print("Original list: ");
printLL(head);
// Creating an instance of Solution class
Solution sol = new Solution();
// Call the deleteKthNode function to delete the k-th node
int k = 2;
head = sol.deleteKthNode(head, k);
// Print the linked list after deletion
System.out.print("List after deleting the kth node: ");
printLL(head);
}
}
# Node structure
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
# Function to delete the k-th node of a linked list
def deleteKthNode(self, head, k):
# If the list is empty, return None
if head is None:
return None
# If k is 1, delete the head node
if k == 1:
head = head.next
return head
# Initialize a temporary pointer
temp = head
# Traverse to the (k-1)th node
for i in range(k - 2):
if temp is None:
break
temp = temp.next
''' If k is greater than the number of nodes,
return the unchanged list '''
if temp is None or temp.next is None:
return head
# Delete the k-th node
temp.next = temp.next.next
# Return head
return head
# Function to print the linked list
def printLL(head):
current = head
while current:
print(current.val, end=" ")
current = current.next
print()
if __name__ == "__main__":
# Initialize a list with values for the linked list
arr = [12, 5, 8, 7]
# Create a linked list with the values from the list
head = ListNode(arr[0])
head.next = ListNode(arr[1])
head.next.next = ListNode(arr[2])
head.next.next.next = ListNode(arr[3])
# Print the original linked list
print("Original list: ", end="")
printLL(head)
# Creating an instance of Solution class
sol = Solution()
# Call the deleteKthNode function to delete the k-th node
k = 2
head = sol.deleteKthNode(head, k)
# Print the linked list after deletion
print("List after deleting the kth node: ", end="")
printLL(head)
// Node structure
class ListNode {
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
}
class Solution {
// Function to delete the k-th node of a linked list
deleteKthNode(head, k) {
// If the list is empty, return null
if (head === null)
return null;
// If k is 1, delete the head node
if (k === 1) {
head = head.next;
return head;
}
// Initialize a temporary pointer
let temp = head;
// Traverse to the (k-1)th node
for (let i = 0; temp !== null && i < k - 2; i++) {
temp = temp.next;
}
/* If k is greater than the number of nodes,
return the unchanged list */
if (temp === null || temp.next === null)
return head;
// Delete the k-th node
temp.next = temp.next.next;
// Return head
return head;
}
}
// Function to print the linked list
function printLL(head) {
let current = head;
while (current !== null) {
process.stdout.write(current.val + " ");
current = current.next;
}
console.log();
}
// Main function
let arr = [12, 5, 8, 7];
// Create a linked list with the values from the array
let head = new ListNode(arr[0]);
head.next = new ListNode(arr[1]);
head.next.next = new ListNode(arr[2]);
head.next.next.next = new ListNode(arr[3]);
// Print the original linked list
console.log("Original list: ");
printLL(head);
// Creating an instance of Solution class
let sol = new Solution();
// Call the deleteKthNode function to delete the k-th node
let k = 2;
head = sol.deleteKthNode(head, k);
// Print the linked list after deletion
console.log("List after deleting the kth node: ");
printLL(head);
Q: What happens if k is out of bounds?
A: If k is greater than the number of nodes, return the original list.
Q: How would you modify this for a circular doubly linked list?
A: If k == 1, update the tail's next to the new head.
Q: How would you modify this to remove all occurrences of a specific value instead of a position?
A: Traverse the list and remove all nodes where val == target, updating pointers accordingly.