Advertisement
snowcava

Nomor 1.a

Apr 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. package nomor01;
  2.  
  3. class Node {
  4. int nilai;
  5. Node next;
  6.  
  7. Node(int nilai) {
  8. this.nilai = nilai;
  9. this.next = null;
  10. }
  11. }
  12.  
  13. public class SingleLinkedList {
  14. static Node head;
  15.  
  16. public static void swapNodes( Node node1, int i) {
  17. Node temp1 = node1.next;
  18. Node temp2 = temp1.next;
  19. temp2.next = temp1;
  20. temp1.next = node1;
  21. node1.next = null;
  22. head = temp2;
  23. }
  24.  
  25. public static void print() {
  26. Node aNode = head;
  27. while (aNode != null) {
  28. System.out.print(aNode.nilai + " ");
  29. aNode = aNode.next;
  30. }
  31. }
  32.  
  33. public static void main(String[] args) {
  34.  
  35. int i = 0;
  36.  
  37. Node node1 = new Node(8);
  38. head = node1;
  39. Node node2 = new Node(6);
  40. Node node3 = new Node(4);
  41. node1.next = node2;
  42. node2.next = node3;
  43.  
  44. swapNodes(node1, i);
  45. print();
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement