|
| 1 | +package collection; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +public class ReverseSingleLinkedList<T> { |
| 8 | + |
| 9 | + private Node<T> head; |
| 10 | + |
| 11 | + public ReverseSingleLinkedList() { |
| 12 | + |
| 13 | + } |
| 14 | + |
| 15 | + public String toString() { |
| 16 | + StringBuffer toReturn = new StringBuffer("["); |
| 17 | + Node<T> tempHead = head; |
| 18 | + if (tempHead != null) { |
| 19 | + toReturn.append(tempHead.getElement()); |
| 20 | + while (tempHead.getNext() != null) { |
| 21 | + tempHead = tempHead.getNext(); |
| 22 | + toReturn.append(", ").append(tempHead.getElement()); |
| 23 | + |
| 24 | + } |
| 25 | + } |
| 26 | + toReturn.append("]"); |
| 27 | + return toReturn.toString(); |
| 28 | + } |
| 29 | + |
| 30 | + public Node<T> getHead() { |
| 31 | + return head; |
| 32 | + } |
| 33 | + |
| 34 | + public void setHead(Node<T> head) { |
| 35 | + this.head = head; |
| 36 | + } |
| 37 | + |
| 38 | + public T add(T data) { |
| 39 | + if (data == null) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + Node<T> n = new Node<T>(data, null); |
| 43 | + Node<T> tempHead = head; |
| 44 | + if (tempHead == null) { |
| 45 | + tempHead = n; |
| 46 | + head = tempHead; |
| 47 | + } else { |
| 48 | + while (tempHead.getNext() != null) { |
| 49 | + tempHead = tempHead.getNext(); |
| 50 | + } |
| 51 | + |
| 52 | + tempHead.setNext(n); |
| 53 | + } |
| 54 | + return data; |
| 55 | + } |
| 56 | + |
| 57 | + public T remove(T data) { |
| 58 | + |
| 59 | + Node<T> tempHead = head; |
| 60 | + Node<T> previous = head; |
| 61 | + |
| 62 | + if (tempHead == null) { |
| 63 | + return null; |
| 64 | + } else { |
| 65 | + |
| 66 | + while (tempHead != null) { |
| 67 | + if (tempHead.getElement().equals(data)) { |
| 68 | + previous.setNext(tempHead.getNext()); |
| 69 | + break; |
| 70 | + } |
| 71 | + previous = tempHead; |
| 72 | + tempHead = tempHead.next; |
| 73 | + } |
| 74 | + } |
| 75 | + return data; |
| 76 | + } |
| 77 | + |
| 78 | + public void reverse() { |
| 79 | + Node<T> prev = null; |
| 80 | + Node<T> current = head; |
| 81 | + Node<T> next = null; |
| 82 | + while (current != null) { |
| 83 | + next = current.next; |
| 84 | + current.next = prev; |
| 85 | + prev = current; |
| 86 | + current = next; |
| 87 | + } |
| 88 | + head = prev; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void test() { |
| 94 | + ReverseSingleLinkedList<String> list = new ReverseSingleLinkedList<>(); |
| 95 | + System.out.println(list); |
| 96 | + |
| 97 | + System.out.println("-----------------------------"); |
| 98 | + |
| 99 | + assertTrue("Hello".equalsIgnoreCase(list.add("Hello"))); |
| 100 | + assertTrue("Saty".equalsIgnoreCase(list.add("Saty"))); |
| 101 | + assertTrue("vir".equalsIgnoreCase(list.add("vir"))); |
| 102 | + assertTrue(null == list.add(null)); |
| 103 | + |
| 104 | + assertTrue("Man".equalsIgnoreCase(list.add("Man"))); |
| 105 | + |
| 106 | + System.out.println(list); |
| 107 | + |
| 108 | + System.out.println("-----------------------------"); |
| 109 | + assertTrue("vir".equalsIgnoreCase(list.remove(("vir")))); |
| 110 | + System.out.println(list); |
| 111 | + |
| 112 | + System.out.println("-----------------------------"); |
| 113 | + list.reverse(); |
| 114 | + System.out.println(list); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + private class Node<T> { |
| 119 | + private T element; |
| 120 | + private Node<T> next; |
| 121 | + |
| 122 | + public T getElement() { |
| 123 | + return element; |
| 124 | + } |
| 125 | + |
| 126 | + public void setElement(T element) { |
| 127 | + this.element = element; |
| 128 | + } |
| 129 | + |
| 130 | + public Node<T> getNext() { |
| 131 | + return next; |
| 132 | + } |
| 133 | + |
| 134 | + public void setNext(Node<T> next) { |
| 135 | + this.next = next; |
| 136 | + } |
| 137 | + |
| 138 | + public Node(T element, Node<T> next) { |
| 139 | + this.element = element; |
| 140 | + this.next = next; |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments