Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class List {
- private static class Node {
- private Node(int v, Node n) {
- val = v;
- next = n;
- }
- private int val;
- private Node next;
- }
- private Node head;
- public List(int rangeOfRandomNumbers, int amountOfRandomNumbers) {
- head = null;
- for (int i = 0; i < amountOfRandomNumbers; i++) {
- addRandom(rangeOfRandomNumbers);
- }
- }
- public List(boolean) {
- head = null;
- }
- public void add(int i) {
- head = new Node(i, head);
- }
- public void addRandom(int r) {
- Random obj = new Random();
- add(obj.nextInt() % r);
- }
- public void del() {
- Node h = head;
- if (h != null) {
- head = head.next;
- h = null;
- }
- }
- public void print() {
- Node h = head;
- while (h != null) {
- System.out.print(h.val + " ");
- h = h.next;
- }
- System.out.println();
- }
- public void reverse() {
- Node cp = head;
- Node np = head.next;
- head.next = null;
- Node pp;
- while (np != null) {
- pp = cp;
- cp = np;
- np = np.next;
- cp.next = pp;
- }
- head = cp;
- }
- public void delRep() {
- Node outCycle = head;
- Node inCycle;
- while (outCycle != null && outCycle.next != null) {
- inCycle = outCycle;
- while (inCycle.next != null) {
- if (inCycle.next.val == outCycle.val)
- inCycle.next = inCycle.next.next;
- else inCycle = inCycle.next;
- }
- outCycle = outCycle.next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement