0% found this document useful (0 votes)
4 views5 pages

LL

The document contains Java code for a linked list implementation, including methods for inserting, deleting, and displaying nodes. It also includes functionality for merging two sorted linked lists and removing duplicates. Additional methods for sorting and reversing the linked list are present, along with a main method demonstrating the usage of the linked list class.

Uploaded by

abhinavsrivas05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

LL

The document contains Java code for a linked list implementation, including methods for inserting, deleting, and displaying nodes. It also includes functionality for merging two sorted linked lists and removing duplicates. Additional methods for sorting and reversing the linked list are present, along with a main method demonstrating the usage of the linked list class.

Uploaded by

abhinavsrivas05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

package com.

kunal; public void insert(int val, int index) {

public class LL { if (index == 0) {

private Node head; insertFirst(val);

private Node tail; return;

private int size; }

public LL() { if (index == size) {

this.size = 0; insertLast(val);

} return;

public void insertFirst(int val) { }

Node node = new Node(val); Node temp = head;

node.next = head; for (int i = 1; i < index; i++) {

head = node; temp = temp.next;

if (tail == null) { }

tail = head; Node node = new Node(val,


temp.next);
}
temp.next = node;
size += 1;
size++;
}
}
public void insertLast(int val) {
// insert using recursion
if (tail == null) {
public void insertRec(int val, int index) {
insertFirst(val);
head = insertRec(val, index, head);
return;
}
}
private Node insertRec(int val, int index,
Node node = new Node(val);
Node node) {
tail.next = node;
if (index == 0) {
tail = node;
Node temp = new Node(val, node);
size++;
size++;
}
return temp;

}
node.next = insertRec(val, index-1, }
node.next);

return node;
public Node find(int value) {
}
Node node = head;
public int deleteLast() {
while (node != null) {
if (size <= 1) {
if (node.value == value) {
return deleteFirst();
return node;
}
}
Node secondLast = get(size - 2);
node = node.next;
int val = tail.value;
}
tail = secondLast;
return null;
tail.next = null;
}
size--;
public Node get(int index) {
return val;
Node node = head;
}
for (int i = 0; i < index; i++) {
public int delete(int index) {
node = node.next;
if (index == 0) {
}
return deleteFirst();
return node;
}
}
if (index == size - 1) {
public int deleteFirst() {
return deleteLast();
int val = head.value;
}
head = head.next;

if (head == null) {
Node prev = get(index - 1);
tail = null;
int val = prev.next.value;
}
prev.next = prev.next.next;
size--;
size--;
return val;
return val;
}
public void display() { }

Node temp = head; }

while (temp != null) { tail = node;

System.out.print(temp.value + " -> "); tail.next = null;

temp = temp.next; }

} //
https://leetcode.com/problems/merge-
System.out.println("END");
two-sorted-lists/submissions/
}
public static LL merge(LL first, LL second)
private class Node { {

private int value; Node f = first.head;

private Node next; Node s = second.head;

public Node(int value) { LL ans = new LL();

this.value = value; while (f != null && s != null) {

} if (f.value < s.value) {

public Node(int value, Node next) { ans.insertLast(f.value);

this.value = value; f = f.next;

this.next = next; } else {

} ans.insertLast(s.value);

} // s = s.next;
https://leetcode.com/problems/remove-
}
duplicates-from-sorted-list
}
public void duplicates() {
while (f != null) {
Node node = head;
ans.insertLast(f.value);
while (node.next != null) {
f = f.next;
if (node.value == node.next.value) {
}
node.next = node.next.next;
while (s != null) {
size--;
ans.insertLast(s.value);
} else {
s = s.next;
node = node.next;
} first.next = second.next;

return ans; second.next = first;

} }

public void bubbleSort() { }

bubbleSort(size - 1, 0); bubbleSort(row, col + 1);

} } else {

private void bubbleSort(int row, int col) { bubbleSort(row - 1, 0);

if (row == 0) { }

return; }

if (col < row) { // recursion reverse

Node first = get(col); private void reverse(Node node) {

Node second = get(col + 1); if (node == tail) {

if (first.value > second.value) { head = tail;

// swap return;

if (first == head) { }

head = second; reverse(node.next);

first.next = second.next; tail.next = node;

second.next = first; tail = node;

} else if (second == tail) { tail.next = null;

Node prev = get(col - 1); }

prev.next = second;

tail = first; // in place reversal of linked list

first.next = null; // google, microsoft, apple, amazon:


https://leetcode.com/problems/reverse-
second.next = tail;
linked-list/
} else {
public void reverse() {
Node prev = get(col - 1);
if (size < 2) {
prev.next = second;
return;
} second.insertLast(14);

Node prev = null;

Node present = head; LL ans = LL.merge(first, second);

Node next = present.next; ans.display();

while (present != null) { LL list = new LL();

present.next = prev; for (int i = 7; i > 0; i--) {

prev = present; list.insertLast(i);

present = next; }

if (next != null) { list.display();

next = next.next; list.bubbleSort();

} list.display();

head = prev; }

public static void main(String[] args) { }

LL first = new LL();

LL second = new LL();

first.insertLast(1);

first.insertLast(3);

first.insertLast(5);

second.insertLast(1);

second.insertLast(2);

second.insertLast(9);

You might also like