0% found this document useful (0 votes)
3 views

Linked List in data structure and algorithm

The document provides Java implementations for various linked list operations, including reversing a list, truncating it, checking if it's sorted, and moving the last node to the front. It also covers doubly linked lists, circular linked lists, and sorting algorithms like bubble sort, selection sort, and insertion sort, along with their time complexities. Additionally, it includes functions for merging sorted linked lists and splitting a sorted list based on a key value.

Uploaded by

dawit kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Linked List in data structure and algorithm

The document provides Java implementations for various linked list operations, including reversing a list, truncating it, checking if it's sorted, and moving the last node to the front. It also covers doubly linked lists, circular linked lists, and sorting algorithms like bubble sort, selection sort, and insertion sort, along with their time complexities. Additionally, it includes functions for merging sorted linked lists and splitting a sorted list based on a key value.

Uploaded by

dawit kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Linked List

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;

while (current != null) {


next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}

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 current = head;


int count = 1;
while (count < n && current != null) {
current = current.next;
count++;
}
if (current != null) {
current.next = null;
}
}
Big-O: O(n)
3. Write a Java implementation of an algorithm that returns true (1) if a list of n integer is sorted
otherwise returns false (0). Assuming linked list data structure. Give also the Big-Oh for your
algorithm.
Ans
1
public boolean isSorted() {
if (head == null) return true;
Node current = head;
while (current.next != null) {
if (current.data > current.next.data) {
return false;
}
current = current.next;
}
return true; // Sorted
}
Big-O: O(n)
4. Implement a function named removeLastAddFirst() that moves the last node of a linked list to the
first position.
Ans
public void removeLastAddFirst() {
if (head == null || head.next == null) return;
Node current = head;
Node prev = null;
while (current.next != null) {
prev = current;
current = current.next;
}
prev.next = null; // Remove the last node
current.next = head; // Point the last node to the current head
head = current; // Update the head
}
5. With the definition of a Doubly Linked List provided, try to give the implementation code for some
of the operations you can perform with a Doubly Linked List
Ans
class DoublyLinkedList {
class Node {
int data;
Node prev;
Node next;

Node(int data) {
this.data = data;
}
}

private Node head;

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;
}
}

private Node head;

// 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;

Node current = head;


Node prev = null;

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);
}

// Search for a node


public boolean search(int data) {
if (head == null) return false;

Node current = head;


do {
if (current.data == data) {
return true;
}
current = current.next;
} while (current != head);
return false;
}
6
}

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

public void splitList(Node head, float key) {

Node list1 = new Node(0);

Node list2 = new Node(0);

Node tail1 = list1;

Node tail2 = list2;

Node current = head;


7
while (current != null) {

if (current.data <= key) {

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

You might also like