Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
Java
class Node: def __init__(self, data): self.data = data self.next = None node1 = Node(3) node2 = Node(5) node3 = Node(13) node4 = Node(2) node1.next = node2 node2.next = node3 node3.next = node4 currentNode = node1 while currentNode: print(currentNode.data, end=" -> ") currentNode = currentNode.next print("null") #Python
public class Main { static class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public static void main(String[] args) { // Creating individual nodes Node firstNode = new Node(3); Node secondNode = new Node(5); Node thirdNode = new Node(13); Node fourthNode = new Node(2); // Linking nodes together firstNode.next = secondNode; secondNode.next = thirdNode; thirdNode.next = fourthNode; // Printing linked list Node currentNode = firstNode; while (currentNode != null) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; } System.out.println("null"); } } //Java
Python result:
Java result:
3 -> 5 -> 13 -> 2 -> null
3 -> 5 -> 13 -> 2 -> null