fork download
  1. public class Main {
  2. public static class Node {
  3. int value;
  4. Node next;
  5.  
  6. public Node(int value) {
  7. this.value = value;
  8. }
  9. }
  10.  
  11. public static class LinkedList {
  12. Node head;
  13.  
  14. public void removeFirstThree() {
  15. // Sprawdź, czy lista nie jest pusta
  16. if (head == null) {
  17. return;
  18. }
  19.  
  20. // Usuń pierwsze trzy elementy
  21. int count = 0;
  22. while (head != null && count < 3) {
  23. head = head.next;
  24. count++;
  25. }
  26. }
  27. }
  28.  
  29. public static void main(String[] args) {
  30. // Przykład użycia
  31. LinkedList list = new LinkedList();
  32. list.head = new Node(1);
  33. list.head.next = new Node(2);
  34. list.head.next.next = new Node(3);
  35. list.head.next.next.next = new Node(4);
  36. list.head.next.next.next.next = new Node(5);
  37.  
  38. System.out.println("Przed usunięciem: ");
  39. printList(list.head);
  40.  
  41. list.removeFirstThree();
  42.  
  43. System.out.println("Po usunięciu: ");
  44. printList(list.head);
  45. }
  46.  
  47. public static void printList(Node head) {
  48. Node current = head;
  49. while (current != null) {
  50. System.out.print(current.value + " ");
  51. current = current.next;
  52. }
  53. System.out.println();
  54. }
  55. }
Success #stdin #stdout 0.12s 48060KB
stdin
Standard input is empty
stdout
Przed usunięciem: 
1 2 3 4 5 
Po usunięciu: 
4 5