Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
class Node: def __init__(self, data): self.data = data self.next = None def traverseAndPrint(head): currentNode = head while currentNode: print(currentNode.data, end=" -> ") currentNode = currentNode.next print("null") def insertNodeAtPosition(head, newNode, position): if position == 1: newNode.next = head return newNode currentNode = head for _ in range(position - 2): if currentNode is None: break currentNode = currentNode.next newNode.next = currentNode.next currentNode.next = newNode return head node1 = Node(7) node2 = Node(3) node3 = Node(2) node4 = Node(9) node1.next = node2 node2.next = node3 node3.next = node4 print("Original list:") traverseAndPrint(node1) # Insert a new node with value 97 at position 2 newNode = Node(97) node1 = insertNodeAtPosition(node1, newNode, 2) print("\nAfter insertion:") traverseAndPrint(node1) #Python
#include
#include
typedef struct Node { int data; struct Node* next; } Node; void traverseAndPrint(Node* head) { Node* currentNode = head; while (currentNode != NULL) { printf("%d -> ", currentNode->data); currentNode = currentNode->next; } printf("null\n"); } Node* insertNodeAtPosition(Node* head, Node* newNode, int position) { if (position == 1) { newNode->next = head; return newNode; } Node* currentNode = head; for (int i = 1; i < position - 1 && currentNode != NULL; i++) { currentNode = currentNode->next; } if (currentNode != NULL) { newNode->next = currentNode->next; currentNode->next = newNode; } return head; } int main() { Node* node1 = malloc(sizeof(Node)); node1->data = 7; Node* node2 = malloc(sizeof(Node)); node2->data = 3; Node* node3 = malloc(sizeof(Node)); node3->data = 2; Node* node4 = malloc(sizeof(Node)); node4->data = 9; node1->next = node2; node2->next = node3; node3->next = node4; printf("Original list:\n"); traverseAndPrint(node1); // Insert a new node with value 97 at position 2 Node* newNode = malloc(sizeof(Node)); newNode->data = 97; node1 = insertNodeAtPosition(node1, newNode, 2); printf("\nAfter insertion:\n"); traverseAndPrint(node1); free(node1); free(node2); free(node3); free(node4); free(newNode); return 0; } //C
public class Main { static class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } public static void traverseAndPrint(Node head) { Node currentNode = head; while (currentNode != null) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; } System.out.println("null"); } public static Node insertNodeAtPosition(Node head, Node newNode, int position) { if (position == 1) { newNode.next = head; return newNode; } Node currentNode = head; for (int i = 1; i < position - 1 && currentNode != null; i++) { currentNode = currentNode.next; } if (currentNode != null) { newNode.next = currentNode.next; currentNode.next = newNode; } return head; } public static void main(String[] args) { Node node1 = new Node(7); Node node2 = new Node(3); Node node3 = new Node(2); Node node4 = new Node(9); node1.next = node2; node2.next = node3; node3.next = node4; System.out.println("Original list:"); traverseAndPrint(node1); // Insert a new node with value 97 at position 2 Node newNode = new Node(97); node1 = insertNodeAtPosition(node1, newNode, 2); System.out.println("\nAfter insertion:"); traverseAndPrint(node1); } } //Java
Python result:
C result:
Java result:
Original list:
7 -> 3 -> 2 -> 9 -> null
After insertion:
7 -> 97 -> 3 -> 2 -> 9 -> null
Original list:
7 -> 3 -> 2 -> 9 -> null
After insertion:
7 -> 97 -> 3 -> 2 -> 9 -> null
Original list:
7 -> 3 -> 2 -> 9 -> null
After insertion:
7 -> 97 -> 3 -> 2 -> 9 -> null