MCPcopy Create free account
hub / github.com/apna-college/Alpha / LL

Class LL

12_LinkedList.java/LL.java:1–343  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1public class LL {
2 public static class Node {
3 int data;
4 Node next;
5 public Node(int data) {
6 this.data = data;
7 this.next = null;
8 }
9 }
10 public static Node head;
11 public static Node tail;
12 public static int size;
13
14 public void addFirst(int data) {
15 size++;
16 Node newNode = new Node(data);
17 if(head == null) {
18 head = tail = newNode;
19 } else {
20 newNode.next = head;
21 head = newNode;
22 }
23 }
24
25 public void addLast(int data) {
26 size++;
27 Node newNode = new Node(data);
28 if(head == null) {
29 head = tail = newNode;
30 } else {
31 tail.next = newNode;
32 tail = newNode;
33 }
34 }
35
36 public void print() {
37 Node temp = head;
38 while(temp != null) {
39 System.out.print(temp.data+"->");
40 temp = temp.next;
41 }
42 System.out.println("null");
43 }
44
45 public void add(int idx, int data) {
46 if(idx == 0) {
47 addFirst(data);
48 return;
49 }
50 size++;
51 Node temp = head;
52 int i = 0;
53 while(temp != null) {
54 if(i == idx-1) {//add here
55 Node newNode = new Node(data);
56 //insertion at middle
57 newNode.next = temp.next;
58 temp.next = newNode;
59 return;
60 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected