运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 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 deleteSpecificNode(head, nodeToDelete): if head == nodeToDelete: return head.next currentNode = head while currentNode.next and currentNode.next != nodeToDelete: currentNode = currentNode.next if currentNode.next is None: return head currentNode.next = currentNode.next.next return head node1 = Node(7) node2 = Node(11) node3 = Node(3) node4 = Node(2) node5 = Node(9) node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node5 print("Before deletion:") traverseAndPrint(node1) # Delete node4 node1 = deleteSpecificNode(node1, node4) print("\nAfter deletion:") 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* deleteSpecificNode(Node* head, Node* nodeToDelete) { if (head == nodeToDelete) { Node* newHead = head->next; free(head); return newHead; } Node* currentNode = head; while (currentNode->next && currentNode->next != nodeToDelete) { currentNode = currentNode->next; } if (currentNode->next == NULL) { return head; } Node* temp = currentNode->next; currentNode->next = currentNode->next->next; free(temp); return head; } int main() { Node* node1 = malloc(sizeof(Node)); node1->data = 7; Node* node2 = malloc(sizeof(Node)); node2->data = 11; Node* node3 = malloc(sizeof(Node)); node3->data = 3; Node* node4 = malloc(sizeof(Node)); node4->data = 2; Node* node5 = malloc(sizeof(Node)); node5->data = 9; node1->next = node2; node2->next = node3; node3->next = node4; node4->next = node5; printf("Before deletion:\n"); traverseAndPrint(node1); node1 = deleteSpecificNode(node1, node4); printf("\nAfter deletion:\n"); traverseAndPrint(node1); free(node1); free(node2); free(node3); free(node5); return 0; } //C
public class Main { static class Node { int data; Node next; 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 deleteSpecificNode(Node head, Node nodeToDelete) { if (head == nodeToDelete) { return head.next; } Node currentNode = head; while (currentNode.next != null && currentNode.next != nodeToDelete) { currentNode = currentNode.next; } if (currentNode.next == null) { return head; } currentNode.next = currentNode.next.next; return head; } public static void main(String[] args) { Node node1 = new Node(7); Node node2 = new Node(11); Node node3 = new Node(3); Node node4 = new Node(2); Node node5 = new Node(9); node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5; System.out.println("Before deletion:"); traverseAndPrint(node1); node1 = deleteSpecificNode(node1, node4); System.out.println("\nAfter deletion:"); traverseAndPrint(node1); } } //Java
Python 结果
C 结果
Java 结果
删除之前
7 -> 11 -> 3 -> 2 -> 9 -> null
删除之后
7 -> 11 -> 3 -> 9 -> null
删除之前
7 -> 11 -> 3 -> 2 -> 9 -> null
删除之后
7 -> 11 -> 3 -> 9 -> null
删除之前
7 -> 11 -> 3 -> 2 -> 9 -> null
删除之后
7 -> 11 -> 3 -> 9 -> null