Last active
April 19, 2026 23:20
-
-
Save kyxap/9b4ace3a6240fdcfec34ff7d4a1c678a to your computer and use it in GitHub Desktop.
One list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package github.kyxap.com.algorithms.test.learn.s2.data; | |
| import lombok.Getter; | |
| import lombok.extern.slf4j.Slf4j; | |
| import static org.junit.jupiter.api.Assertions.assertEquals; | |
| //ДЗ Однозвязний список | |
| //1.Написати пошук елемента в списку - рекурсія | |
| //2.Пошук середини списку - Fast & Slow Pointers | |
| //3.Додавання в голову списку - Push | |
| //4.Видалення з голови - Pop | |
| //5.Видалення елементу зі списка(4 - випадки) | |
| @Slf4j | |
| public class OneList { | |
| @Getter | |
| private Node head; | |
| @Getter | |
| private Node tail; | |
| public void add(int val) { | |
| Node curr = new Node(val, null); | |
| if (head == null) { | |
| head = tail = curr; | |
| } else { | |
| tail.next = curr; | |
| tail = tail.next; | |
| } | |
| } | |
| @Deprecated | |
| public void showAllOld() { | |
| // Node curr = head; | |
| // while (curr != null) { | |
| // log.info("{}", curr.val); | |
| // curr = curr.right; | |
| // } | |
| for (Node curr = head; curr != null; curr = curr.next) { | |
| log.info("{}", curr.val); | |
| } | |
| } | |
| public void showAll() { | |
| showAllRecPre(getHead()); | |
| } | |
| public void showAllRecPre(Node curr) { | |
| if (curr == null) { | |
| return; | |
| } else { | |
| log.info("{}", curr.val); | |
| showAllRecPre(curr.next); | |
| } | |
| } | |
| public void showAllRecPost(Node curr) { | |
| if (curr == null) { | |
| return; | |
| } else { | |
| showAllRecPost(curr.next); | |
| log.info("{}", curr.val); | |
| } | |
| } | |
| @Deprecated | |
| public Node findVal(int val) { | |
| Node curr = head; | |
| while (curr != null && curr.val != val) { | |
| curr = curr.next; | |
| } | |
| return curr; | |
| } | |
| public Node findValRec(int val, Node curr) { | |
| if (curr == null || curr.val == val) { | |
| return curr; | |
| } else { | |
| return findValRec(val, curr.next); | |
| } | |
| } | |
| @Deprecated | |
| // todo find midle using fast/slo algo | |
| public Node findMidFakeLoop() { | |
| if (exist(head) && exist(head.next)) { | |
| return slowAndFindSearchLoop(head, head.next.next); | |
| } else { | |
| //log.warn("Head is null"); | |
| return head; | |
| } | |
| } | |
| @Deprecated | |
| // TODO: була проблема, шо думав що швидкий перестрибне повільний і я на парному списку отримаю Null | |
| // Використав loop для пошуку, не працюе так і не зрозумів чи можно його доробити шо б повністью все прцювало | |
| public Node slowAndFindSearchLoop(Node slow, Node fast) { | |
| if (slow == null) { | |
| return null; | |
| } | |
| if (slow == fast) { | |
| return slow; | |
| } else { | |
| if (fast == null || fast.next == null) { // aka tail | |
| fast = head; | |
| } else { | |
| fast = fast.next.next; | |
| } | |
| return slowAndFindSearchLoop(slow.next, fast); | |
| } | |
| } | |
| //5.Видалення елементу зі списка(4 - випадки) | |
| public Node findMid() { | |
| if (exist(head) && exist(head.next)) { | |
| return slowAndFindSearch(head, head); | |
| } else { | |
| //log.warn("Head is null"); | |
| return head; | |
| } | |
| } | |
| private Node slowAndFindSearch(Node slow, Node fast) { | |
| if (fast == null || fast.next == null) { | |
| return slow; | |
| } else | |
| return slowAndFindSearch(slow.next, fast.next.next); | |
| } | |
| // todo remove/dete 4 diff state | |
| //5.Видалення елементу зі списка(4 - випадки) | |
| // [x] - no head - false | |
| // [x] - no element - false | |
| // [x] - in the middle | |
| // [x] - in the beginning | |
| // [x] - in th end | |
| public boolean delete(int valNodeToDelete) { | |
| if (!exist(getHead())) { | |
| log.info("Head is null, nothing to delete"); | |
| return false; | |
| } | |
| // if head val to delete | |
| if (head.val == valNodeToDelete) { | |
| head = head.next; | |
| if (head == null) tail = head; | |
| return true; | |
| } | |
| //Node prev = findPrev(getHead(), getHead().next, valNodeToDelete); | |
| Node prev = findPrevClean(getHead(), valNodeToDelete); | |
| if (prev == null) { | |
| return false; | |
| } else { | |
| if (prev.next.next == null) { | |
| prev.next = null; | |
| tail = prev; | |
| } else { | |
| prev.next = prev.next.next; | |
| } | |
| return true; | |
| } | |
| } | |
| @Deprecated | |
| private Node findPrev(Node prev, Node curr, int target) { | |
| if (curr == null) { | |
| return null; | |
| } else { | |
| if (curr.val == target) { | |
| return prev; | |
| } else { | |
| return findPrev(curr, curr.next, target); | |
| } | |
| } | |
| } | |
| // refactored after find implementation | |
| private Node findPrevClean(Node curr, int target) { | |
| if (curr == null || curr.next == null) { | |
| return null; | |
| } | |
| Node next = curr.next; | |
| if (next.val == target) { | |
| return curr; | |
| } else { | |
| return findPrevClean(curr.next, target); | |
| } | |
| } | |
| //1.Написати пошук елемента в списку - рекурсія | |
| public Node find(int val) { | |
| return find(head, val); | |
| } | |
| private Node find(Node curr, int val) { | |
| if (curr == null || curr.val == val) { | |
| return curr; | |
| } else { | |
| return find(curr.next, val); | |
| } | |
| } | |
| //3.Додавання в голову списку - Push | |
| public void push(int val) { | |
| if (exist(getHead())) { | |
| Node oldHead = getHead(); | |
| Node newHead = new Node(val, oldHead); | |
| head = newHead; | |
| } else { | |
| head = new Node(val, null); | |
| tail = head; | |
| } | |
| } | |
| //4.Видалення з голови - Pop | |
| // TODO: питання | |
| // - відрази чи залишати? | |
| public Node pop() { | |
| Node curr = head; | |
| if (exist(curr)) { | |
| head = head.next; | |
| if (!exist(head)) { | |
| tail = head; | |
| } | |
| } | |
| return curr; | |
| } | |
| public Node peek() { | |
| return getHead(); | |
| } | |
| private boolean exist(Node node) { | |
| return node != null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment