Implement Linked List operation. You can not use existing List APIs. Please impl
ID: 3903300 • Letter: I
Question
Implement Linked List operation. You can not use existing List APIs. Please implement the belowing operations:
(A) appendSortedDESC(ListNode head, int val): creates a new node with the integer value and inserts it to the linked list in a location to maintain the descending order. If the list is not already sorted, new node can be inserted anywhere.
Hint : There is already a code with appendSortedAsc which maintains the ascending order. You need to do the descending one.
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 appendDESC method . Please write your code here
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));
}
}
Explanation / Answer
Given below is the completed code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
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;
}
}
}
public void sortedAppendACS(ListNode head, int val) {
ListNode tmp = new ListNode(val);
if (head.val >= val)
{
ListNode headcopy = new ListNode(head.val);
headcopy.next = head.next;
head.next = headcopy;
headcopy = head.next;
head.val = val;
return;
}
while (head.next != null && head.next.val <= val)
head = head.next;
tmp.next = head.next;
head.next = tmp;
return;
}
//This is where you will be implementing appendDESC method . Please write your code here
public void sortedAppendDESC(ListNode head, int val) {
ListNode tmp = new ListNode(val);
if (head.val <= val)
{
ListNode headcopy = new ListNode(head.val);
headcopy.next = head.next;
head.next = headcopy;
headcopy = head.next;
head.val = val;
return;
}
while (head.next != null && head.next.val >= val)
head = head.next;
tmp.next = head.next;
head.next = tmp;
return;
}
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));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.