Question: Implement Linked List operations. You can not use existing List APIs. Please implement the belowing operations: (a) isNull(ListNode head): return true if the linked list

Implement Linked List operations. You can not use existing List APIs. Please implement the belowing operations: (a) isNull(ListNode head): return true if the linked list is NULL; else false ?(b) insert(ListNode head, int n, int val): insert a new node to the nth position, the value of the new node equals to val. ?(c) remove(ListNode head, int n): remove the nth node (d) removeVal(ListNode head, int val): remove all the nodes that have value equal to val. ?Java file is below

class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public class LinkedListOperations { public int getSize(ListNode head){ int size = 0; while(head!=null){ size += 1; head = head.next; } return size; } public void display(ListNode head) { if (head == null) { System.out.println(""); return; } int n = getSize(head); while (head != null) { if (n-- != 1 ) { System.out.print(head.val + "->"); head = head.next; } else { System.out.println(head.val); head = head.next; } } } //This is where you will be implementing 4 methods public static void main(String[] args){ ListNode l1 = new ListNode(5); ListNode l2 = new ListNode(2); ListNode l3 = new ListNode(3); ListNode l4 = new ListNode(2); ListNode l5 = new ListNode(4); l1.next = l2; l2.next = l3; l3.next = l4; l4.next = l5; LinkedListOperations l = new LinkedListOperations(); l.display(l1); System.out.println("The size of LinkedList l1 is: " + l.getSize(l1)); l.display(l5); System.out.println("The size of LinkedList l5 is: " + l.getSize(l5)); l.display(null); System.out.println("The size of empty LinkedList is: " + l.getSize(null)); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!