Linked List in data structure and algorithm
Linked List in data structure and algorithm
Java
1. Implement a function named reverseList() as a new function for the linked list toolkit. The function
should reverse the list.
Ans
public void reverseList() {
Node prev = null;
Node current = head;
Node next = null;
2. Implement a function named Truncate() that keeps only the first n node of a linked list and remove
the rest. Truncation is done only if the number of nodes in the linked list is greater than n. You Give
also the Big-Oh for your algorithm.
Ans
public void truncate(int n) {
if (head == null || n <= 0) return;
Node(int data) {
this.data = data;
}
}
2
private Node tail;
public void insertEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}
public void delete(int data) {
Node current = head;
while (current != null) {
if (current.data == data) {
if (current.prev != null) {
current.prev.next = current.next;
} else {
head = current.next;
}
if (current.next != null) {
current.next.prev = current.prev;
} else {
tail = current.prev;
}
return;
}
current = current.next;
}
}
}
6. Implement bubble sort for a list of integers based on linked list structure Compare the complexity of
your algorithm with that of an array structure. On which structure bubble sort is efficient? Give also
the Big-Oh for your algorithm.
Ans
public void bubbleSort() {
if (head == null) return;
boolean swapped;
Node current;
Node last = null;
do {
swapped = false;
3
current = head;
while (current.next != last) {
if (current.data > current.next.data) {
// Swap data
int temp = current.data;
current.data = current.next.data;
current.next.data = temp;
swapped = true;
}
current = current.next;
}
last = current;
} while (swapped);
}
Big-O: O(n²)
7. Repeat the previous problem for selection sort.
Ans
public void selectionSort() {
Node current = head;
while (current != null) {
Node min = current;
Node temp = current.next;
while (temp != null) {
if (temp.data < min.data) {
min = temp;
}
temp = temp.next;
}
int tempData = current.data;
current.data = min.data;
min.data = tempData;
current = current.next;
}
}
Big-O: O(n²)
8. Repeat the previous problem for insertion sort. assuming doubly linked list structure.
Ans
public void insertionSort() {
if (head == null || head.next == null) return;
Node current = head.next;
while (current != null) {
Node key = current;
Node prev = current.prev;
4
while (prev != null && prev.data > key.data) {
prev.next.data = prev.data;
prev = prev.prev;
}
if (prev == null) {
head.data = key.data;
} else {
prev.next.data = key.data;
}
current = current.next;
}
}
Big-O: O(n²)
9. Give the implementation of the following functions for a circular linked list. A circular linked list is
one in which the next field for the last link node of the list points to the first link node of the list.
a. insertNode( ) b. deleteNode( ) c. Search( )
Ans
class CircularLinkedList {
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
// Insert a node
public void insertNode(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
head.next = head;
} else {
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = newNode;
5
newNode.next = head;
}
}
// Delete a node
public void deleteNode(int data) {
if (head == null) return;
do {
if (current.data == data) {
if (prev != null) {
prev.next = current.next;
} else {
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = head.next;
head = head.next;
}
return;
}
prev = current;
current = current.next;
} while (current != head);
}
10. Give the implementation of the following functions for a linked list whose data values are float.
a. Merge two linked list assuming the lists are sorted. The resulting list should be also sorted.
Ans
public Node mergeSortedLists(Node head1, Node head2) {
Node dummy = new Node(0);
Node tail = dummy;
while (head1 != null && head2 != null) {
if (head1.data <= head2.data) {
tail.next = head1;
head1 = head1.next;
} else {
tail.next = head2;
head2 = head2.next;
}
tail = tail.next;
}
if (head1 != null) {
tail.next = head1;
} else {
tail.next = head2;
}
return dummy.next;
}
b. Split a sorted linked list into two sorted linked list in which one of the list contains all the
elements less than or equal to a certain key value.
Ans
tail1.next = current;
tail1 = tail1.next;
} else {
tail2.next = current;
tail2 = tail2.next;
current = current.next;
tail1.next = null;
tail2.next = null;
// list1.next and list2.next are the heads of the two new lists