运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 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 结果
Java 结果
3 -> 5 -> 13 -> 2 -> null
3 -> 5 -> 13 -> 2 -> null