Section Problems Number 10 - Linked List Operations.

Be prepared to discuss these problems in discussion section.
Thanks to Owen Astrachan of Duke University for ideas and materials.

Assume a singly linked list is implemented using singly linked nodes. Refer to these classes:

When writing methods try to implement them without calling other methods. The purpose of these exercises is to become comfortable with working with dynamic data structures.

1. Write a method for the SinglyLinkedList class chopSkip that removes every other node (keeping the first, third, ... nodes, removing the second, fourth, ...) from a list. The list

   ("aardvark", "bear", "cougar", "dog", "elephant", "fox")

should become

   ("aardvark", "cougar", "elephant")
//target Big O -> O(N)
public void chopSkip()


2.  Write a method for the SinglyLinkedList class numberPresent. This method counts the number of elements in a linked list equal to a given parameter. For example if the linked list contained strings and held the following values:

("pinto", "bean", "lima", "bean", "nota", "bene")

A call to list.numberPresent("bean") would return 2.

// target Big O -> O(N)
public int numberPresent(Object x)



A. Write a method  for the SinglyLinkedList class hasDuplicates whose header follows. The method returns true if parameter list has any duplicates (words occurring more than once) and false otherwise. For example, for the list

( "apple", "guava", "cherry")

hasDuplicates should return false, but it would return true for the list below since "guava" appears twice.

( "apple", "guava", "cherry", "guava")

In writing hasDuplicates you must call the method countOccurrences shown below and your method must be either a recursive function with no loop or a function with one loop. Either version can include calls to countOccurrences. You cannot use any ArrayList, Set, etc. objects in your code.

* @return the number of occurrences of s in list
private int countOccurrences(Node temp, Object s)
{   if (temp == null)
        return 0;
    int count = 0;
    if ( temp.getData().equals( s ) )
        count = 1;
    return count + countOcurrences( temp.getData(),s );

* @return true if and only if list has duplicates
public boolean hasDuplicates()

B. What is the Big O of each of the methods you wrote?

C. If you are not required to use countOccurrences in your hasDuplicates can you find a more efficient way of implementing hasDuplicates?


4. We have implemented singly linked lists in class with nodes and a list class that keeps track of the first node in the list, myHead, and the last node in the list, myTail. Assume a singly linked list is implemented with only a reference to the first node in the list, myHead. How would you determine if there were a cycle in the list? A cycle exists if the "last" node has its next reference set to another Node in the list instead of null. Here is a picture of a singly linked list with a cycle in it.

Write a method  for the SinglyLinkedList class hasCycle.

private boolean hasCycle()
// assume SinglyLinkedList only has myHead. No myTail or mySize instance variables.