Given the head of a singly linked list and an integer X, insert a node with value X at the tail of the linked list and return the head of the modified list.
The tail is the last node of the linked list.
Input: head -> 1 -> 2 -> 3, X = 7
Output: head -> 1 -> 2 -> 3 -> 7
Explanation: 7 was added as the last node.
Input: head, X = 0
Output: head -> 0
Explanation: 0 was added as the last/only node.
Input: head -> 5 -> 6, X = 8
NULL
.#include <bits/stdc++.h>
using namespace std;
// Definition of singly linked list
struct ListNode
{
int val;
ListNode *next;
ListNode(int data1)
{
val = data1;
next = NULL;
}
ListNode(int data1, ListNode *next1)
{
val = data1;
next = next1;
}
};
// Solution class
class Solution {
public:
// Function to insert a new node at the tail of the linked list
ListNode* insertAtTail(ListNode* &head, int X) {
if (head == NULL)
return new ListNode(X);
ListNode* temp = head;
// Traversing until the last node
while (temp->next != NULL) {
temp = temp->next;
}
ListNode* newNode = new ListNode(X);
temp->next = newNode;
return head;
}
};
// Helper Function to print the linked list
void printLL(ListNode* head) {
while (head != NULL) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
int main() {
// Create a linked list from a vector
vector<int> arr = {10, 20, 30};
int val = 40;
ListNode* head = new ListNode(arr[0]);
head->next = new ListNode(arr[1]);
head->next->next = new ListNode(arr[2]);
// Print the original list
cout << "Original List: ";
printLL(head);
// Create a Solution object
Solution sol;
head = sol.insertAtTail(head, val);
// Print the modified linked list
cout << "List after inserting the given value at the tail:";
printLL(head);
return 0;
}
import java.util.*;
// Definition of singly linked list
class ListNode {
int val;
ListNode next;
ListNode(int data1) {
val = data1;
next = null;
}
ListNode(int data1, ListNode next1) {
val = data1;
next = next1;
}
}
// Solution class
class Solution {
// Function to insert a new node at the tail of the linked list
public ListNode insertAtTail(ListNode head, int X) {
if (head == null)
return new ListNode(X);
ListNode temp = head;
// Traversing until the last node
while (temp.next != null) {
temp = temp.next;
}
ListNode newNode = new ListNode(X);
temp.next = newNode;
return head;
}
}
// Main class
class Main {
// Helper Function to print the linked list
private static void printLL(ListNode head) {
while (head != null) {
System.out.print(head.val + " ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a linked list from an array
int[] arr = {10, 20, 30};
int val = 40;
ListNode head = new ListNode(arr[0]);
head.next = new ListNode(arr[1]);
head.next.next = new ListNode(arr[2]);
// Print the original list
System.out.print("Original List: ");
printLL(head);
// Create a Solution object
Solution sol = new Solution();
head = sol.insertAtTail(head, val);
// Print the modified linked list
System.out.print("List after inserting the given value at the tail: ");
printLL(head);
}
}
# Definition of singly linked list
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
# Function to insert a new node at the tail of the linked list
def insertAtTail(self, head, X):
if head is None:
return ListNode(X)
temp = head
# Traversing until the last node
while temp.next is not None:
temp = temp.next
newNode = ListNode(X)
temp.next = newNode
return head
# Helper Function to print the linked list
def printLL(head):
while head is not None:
print(head.val, end=" ")
head = head.next
print()
if __name__ == "__main__":
# Create a linked list from a list
arr = [10, 20, 30]
val = 40
head = ListNode(arr[0])
head.next = ListNode(arr[1])
head.next.next = ListNode(arr[2])
# Print the original list
print("Original List: ", end="")
printLL(head)
# Create a Solution object
sol = Solution()
head = sol.insertAtTail(head, val)
# Print the modified linked list
print("List after inserting the given value at the tail: ", end="")
printLL(head)
// Definition of singly linked list
class ListNode {
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
}
// Solution class
class Solution {
// Function to insert a new node at the tail of the linked list
insertAtTail(head, X) {
if (head === null)
return new ListNode(X);
let temp = head;
// Traversing until the last node
while (temp.next !== null) {
temp = temp.next;
}
let newNode = new ListNode(X);
temp.next = newNode;
return head;
}
}
// Helper 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 = [10, 20, 30];
let val = 40;
let head = new ListNode(arr[0]);
head.next = new ListNode(arr[1]);
head.next.next = new ListNode(arr[2]);
// Print the original list
console.log("Original List: ");
printLL(head);
// Create a Solution object
let sol = new Solution();
head = sol.insertAtTail(head, val);
// Print the modified linked list
console.log("List after inserting the given value at the tail:");
printLL(head);
Q: What is the difference between inserting at the tail and other positions?
A: Inserting at the tail requires traversal to the end of the list (unless the tail is tracked separately), while inserting at other positions might require locating a specific index or node.
Q: How do you ensure the new node is added at the correct position?
A: By traversing the list until the tail (node with next = None) is found and updating its next pointer to point to the new node.
Q: What if you frequently need to insert at the tail?
A: Maintain a direct reference to the tail node, allowing O(1) insertion without requiring traversal.
Q: What if the list needs to remain sorted after insertion?
A: Traverse the list to find the correct position based on X’s value, and insert the new node at that position.