---
# System prepended metadata

title: Linkedlist

---

# Linkedlist


```java=
package node;

public class Node {
	
	int value;
	Node nextNode;
	
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public Node getNextNode() {
		return nextNode;
	}
	public void setNextNode(Node nextNode) {
		this.nextNode = nextNode;
	}
	/**
	 * @param value
	 * @param nextNode
	 */
	public Node(int value, Node nextNode) {
		super();
		this.value = value;
		this.nextNode = nextNode;
	}
	/**
	 * @param value
	 */
	public Node(int value) {
		super();
		this.value = value;
	}
	@Override
	public String toString() {
		return "Node [value=" + value + ", nextNode=" + nextNode + "]";
	}
	
	void printsingleNode(Node node) {
		System.out.println(node.getValue());
	}
	
	public static  void  print_all_Node(Node node) {
		
		 Node currentNode = node;
		 
	     while (currentNode != null) {
	          System.out.print(currentNode.value + " ");
	          currentNode = currentNode.nextNode; //move current node to its nextNode
	      }
	     System.out.println("");
	}
	
	public static void insertNodeatHead(Node head,Node value) {
		value.setNextNode(head);
	}
	
}

```

```java=
public class DriverClass {
	public static void main(String[] args) {
		Node firstNode = new Node(100);
		Node secondNode = new Node(200);
		Node thirdNode = new Node(300);
		Node fourthNode = new Node(400);
		System.out.println("firstNode value: " +firstNode.getValue());
		System.out.println("尚未建立鏈結時: firstNode的下一個Node是: "+firstNode.getNextNode());
		System.out.println("開始建立第一個Node和第二個Node的鏈結");
		firstNode.setNextNode(secondNode);	
		System.out.println("建立鏈結時: firstNode的下一個Node是: "+   firstNode.getNextNode());	
		System.out.println(secondNode.getNextNode());
		System.out.println("開始建立第二個Node和第三個Node的鏈結: ");
		secondNode.setNextNode(thirdNode);
		System.out.println(firstNode.getNextNode());		
		System.out.println(secondNode.getNextNode());
		thirdNode .setNextNode(fourthNode);
		System.out.println("打印 firstNode.getNextNode()");	
		System.out.println(firstNode.getNextNode());	
		System.out.println("打印 firstnode");	
		Node.print_all_Node(firstNode);
		Node ZerotNode = new Node(500);
		Node.insertNodeatHead(firstNode,ZerotNode );
		System.out.println("11111111111111111");
		Node.print_all_Node(ZerotNode);
		System.out.println( ZerotNode   );
	}
}
```

![](https://i.imgur.com/oZHUeca.png)



