Professional Documents
Culture Documents
Mahnoor Iftikhar
Home Assignment 2
SINGLY LINKED LIST IMPLEMENTATION
Task # 1:
import java.util.*;
if (ans == true)
else
// addition of a element to
// the List
arr.add(1);
ans = arr.isEmpty();
if (ans == true)
else
Task # 2:
Implement the getSize() method in the Linked List
import java.io.*;
import java.util.LinkedList;
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
System.out.println("LinkedList:" + list);
+ list.size());
Task#3
//
// CASE 1:
return list;
//
// CASE 2:
// If the index is greater than 0 but less than the size of LinkedList
//
// The counter
int counter = 0;
if (counter == index) {
prev.next = currNode.next;
break;
else {
// If current position is not the index
prev = currNode;
currNode = currNode.next;
counter++;
//
//
if (currNode == null) {
return list;
Task # 4:
{
Node newNode=new Node(0,null);
newNode.data = element;
newNode.next = null;
while(temp.next != null)
temp = temp.next;
temp.next = newNode;
//return head;
Task#5
class Node{
int data;
Node next;
this.data = data;
this.next = null;
if(head == null) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
tail = newNode;
Task#6