运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
转到 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 <stdio.h> #include <stdlib.h> 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 结果
C 结果
Java 结果
原始列表
7 -> 3 -> 2 -> 9 -> null
插入后
7 -> 97 -> 3 -> 2 -> 9 -> null
原始列表
7 -> 3 -> 2 -> 9 -> null
插入后
7 -> 97 -> 3 -> 2 -> 9 -> null
原始列表
7 -> 3 -> 2 -> 9 -> null
插入后
7 -> 97 -> 3 -> 2 -> 9 -> null