Insert a node at a specific position in a linked list

Sort by

recency

|

1548 Discussions

|

  • + 0 comments

    Funnily enough the way to make the code run properly was to remove the result class as the main function they did not call the result class, here is my code import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.*;

    public class Solution {

    static class SinglyLinkedListNode {
        public int data;
        public SinglyLinkedListNode next;
    
        public SinglyLinkedListNode(int nodeData) {
            this.data = nodeData;
            this.next = null;
        }
    }
    
    static class SinglyLinkedList {
        public SinglyLinkedListNode head;
        public SinglyLinkedListNode tail;
    
        public SinglyLinkedList() {
            this.head = null;
            this.tail = null;
        }
    
        public void insertNode(int nodeData) {
            SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
    
            if (this.head == null) {
                this.head = node;
            } else {
                this.tail.next = node;
            }
    
            this.tail = node;
        }
    }
    
    public static void printSinglyLinkedList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException {
        while (node != null) {
            bufferedWriter.write(String.valueOf(node.data));
    
            node = node.next;
    
            if (node != null) {
                bufferedWriter.write(sep);
            }
        }
    }
    
    
    /*
     * Complete the 'insertNodeAtPosition' function below.
     *
     * The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
     * The function accepts following parameters:
     *  1. INTEGER_SINGLY_LINKED_LIST llist
     *  2. INTEGER data
     *  3. INTEGER position
     */
    
    /*
     * For your reference:
     *
     * SinglyLinkedListNode {
     *     int data;
     *     SinglyLinkedListNode next;
     * }
     *
     */
    
    public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
    
    SinglyLinkedListNode newNode = new SinglyLinkedListNode(data);
    
    if(position==0) {
        newNode.next = llist;
        return newNode;
    }
    
    SinglyLinkedListNode curr = llist;
    for (int i = 0; i < position - 1 && curr != null; ++i) {
        curr = curr.next;
    }
    
    newNode.next=curr.next;
    curr.next=newNode;
    
    return llist;
    }
    
    
    private static final Scanner scanner = new Scanner(System.in);
    
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
        SinglyLinkedList llist = new SinglyLinkedList();
    
        int llistCount = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
        for (int i = 0; i < llistCount; i++) {
            int llistItem = scanner.nextInt();
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
            llist.insertNode(llistItem);
        }
    
        int data = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
        int position = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
        SinglyLinkedListNode llist_head = insertNodeAtPosition(llist.head, data, position);
    
        printSinglyLinkedList(llist_head, " ", bufferedWriter);
        bufferedWriter.newLine();
    
        bufferedWriter.close();
    
        scanner.close();
    }
    

    }

  • + 0 comments

    This code is bugged if you try coding in Java. I tried changing result class to static, and then tried changing main function, but ultimately the fix was to take the required implementation out of the result class.

  • + 0 comments

    Data structures are the backbone of programming! 📚 They help organize and manage data efficiently, making algorithms faster and code more optimized. best exchange id for betting

  • + 0 comments

    Here's a solution in C#

    static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position)
        {
    		SinglyLinkedListNode node = new SinglyLinkedListNode(data);
            
            if(position == 0){
                node.next = llist;
                return node;
            }
            
            SinglyLinkedListNode cur = llist;
            
            for(int i = 0; i < position -  1; i++){
                if(cur != null){
                    cur = cur.next;    
                }
            }
            
            node.next = cur.next;
            cur.next = node;
            
    		return llist;
        }
    
  • + 0 comments

    The following code attempts to insert a node into a singly linked list at a specific position. Comment on the correctness of the logic. Identify any edge cases that may not be handled and suggest improvements if necessary. Bet Guru