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 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
#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* 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 result:
C result:
Java result:
Before deletion:
7 -> 11 -> 3 -> 2 -> 9 -> null
After deletion:
7 -> 11 -> 3 -> 9 -> null
Before deletion:
7 -> 11 -> 3 -> 2 -> 9 -> null
After deletion:
7 -> 11 -> 3 -> 9 -> null
Before deletion:
7 -> 11 -> 3 -> 2 -> 9 -> null
After deletion:
7 -> 11 -> 3 -> 9 -> null