Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problems: Reverse a singly linked list Remove duplicated in a sorted linked list Submission First, look at the following checklist: Have you tested every method?

Problems:
Reverse a singly linked list
Remove duplicated in a sorted linked list
Submission First, look at the following checklist:
Have you tested every method?
Is the indentation easily readable? You can have Eclipse correct indentation by highlighting all
code and select "Source > Correct Indentation".
Are comments well organized and concise?
Grading Criteria
Sort an array (20p)
Find the kth largest element in an array, not sorted (15p)
Reverse a singly linked list (20p)
Remove duplicated in a sorted linked list (20p)
Complete the 4 unimplemented test cases (3 in ArrayProblemsTest.java and 1 in LinkedListProb-
lemsTest.java)(20p)
Source code is appropriately commented (5p)
public class LinkedListProblems {
/*
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
*/
public static ListNode deleteDuplicates(ListNode head){
//TODO: finish this method.
//TODO: Modify this line to return correct data.
return null;
}
/*
* Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
*/
public static ListNode reverseList(ListNode head){
//TODO:finish this method.
//TODO: Modify this line to return correct data.
return null;
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
public class LinkedListProblemsTest {
ListNode T;
/* The common test data to be used is the list T: 1->1->2->3->3
*/
@Before
public void setUp() throws Exception {
int[] testdata={1,1,2,3,3};
ListNode T = new ListNode(testdata[1]);
for (int i=1; i1->2->3->3->null
Output: 1->2->3->null
*/
@Test
public void testDeleteDuplicates(){
int[] expected ={1,2,3};
ListNode newT=LinkedListProblems.deleteDuplicates(T);
ListNode p=newT;
int i =0;
while (p!=null){
assertEquals(expected[i], p.val);
i = i+1;
}
}
/*
* Reverse a singly linked list.
Example:
Input: 1->1->2->3->3->NULL
Output: 3->3->1->1->1->NULL
*/
@Test
public void testReverseList(){
fail("Not yet implemented"); // TODO
}
}
public class ListNode {
int val;
ListNode next;
ListNode(int x){ val = x; }
}
Please Complete them

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

Explain how a gold standard, as monetary policy, would work?

Answered: 1 week ago