Question: //Following codes are in Java public class LinkedListProblems { /* Given a sorted linked list, delete all duplicates such that each element appear only once.
//Following codes are in Java
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; } }
------------------------------------------------------------------------------------------------------------ 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; i /* Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->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; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
