Untitled

 avatar
unknown
plain_text
18 days ago
3.4 kB
4
Indexable
class ListNode {
    int val;
    ListNode next;

    ListNode(int val) {
        this.val = val;
        this.next = null;
    }
}

public class LinkedListOperations {

    // Insert at kth position in Linked List
    public ListNode insertAtKthPosition(ListNode head, int k, int value) {
        ListNode newNode = new ListNode(value);

        // Insert at the beginning
        if (k == 1) {
            newNode.next = head;
            return newNode;
        }

        ListNode current = head;
        int position = 1;

        // Traverse to (k-1)th position
        while (current != null && position < k - 1) {
            current = current.next;
            position++;
        }

        // Invalid position check
        if (current == null) {
            System.out.println("Position out of bounds");
            return head;
        }

        // Insert new node
        newNode.next = current.next;
        current.next = newNode;

        return head;
    }

    // Delete at kth position in Linked List
    public ListNode deleteAtKthPosition(ListNode head, int k) {
        // Delete at the beginning
        if (k == 1) {
            if (head == null) return null; // If list is empty
            return head.next;
        }

        ListNode current = head;
        int position = 1;

        // Traverse to (k-1)th position
        while (current != null && position < k - 1) {
            current = current.next;
            position++;
        }

        // Invalid position check
        if (current == null || current.next == null) {
            System.out.println("Position out of bounds");
            return head;
        }

        // Delete the kth node
        current.next = current.next.next;

        return head;
    }

    // Helper Method: Print Linked List
    public void printLinkedList(ListNode head) {
        ListNode current = head;

        while (current != null) {
            System.out.print(current.val + " -> ");
            current = current.next;
        }
        System.out.println("null");
    }

    // Main Method: Test All Operations
    public static void main(String[] args) {
        LinkedListOperations operations = new LinkedListOperations();

        // Create a Linked List
        int[] arr = {10, 20, 30, 40, 50};
        ListNode head = operations.arrayToLinkedList(arr);

        System.out.print("Original Linked List: ");
        operations.printLinkedList(head);

        // Insert at kth position
        head = operations.insertAtKthPosition(head, 3, 25); // Insert 25 at position 3
        System.out.print("After Insertion at Position 3: ");
        operations.printLinkedList(head);

        // Delete at kth position
        head = operations.deleteAtKthPosition(head, 4); // Delete node at position 4
        System.out.print("After Deletion at Position 4: ");
        operations.printLinkedList(head);
    }

    // Convert Array to Linked List
    public ListNode arrayToLinkedList(int[] arr) {
        if (arr == null || arr.length == 0) return null;

        ListNode head = new ListNode(arr[0]);
        ListNode current = head;

        for (int i = 1; i < arr.length; i++) {
            current.next = new ListNode(arr[i]);
            current = current.next;
        }

        return head;
    }
}
Editor is loading...
Leave a Comment