Question d’entretien chez LinkedIn

Reverse a linked list

Réponses aux questions d'entretien

Utilisateur anonyme

23 août 2015

public void ReverseLinkedList (LinkedList linkedList) { // ------------------------------------------------------------ // Create a new linked list and add all items of given // linked list to the copy linked list in reverse order // ------------------------------------------------------------ LinkedList copyList = new LinkedList(); // ------------------------------------------------------------ // Start from the latest node // ------------------------------------------------------------ LinkedListNode start = linkedList.Tail; // ------------------------------------------------------------ // Traverse until the first node is found // ------------------------------------------------------------ while (start != null) { // ------------------------------------------------------------ // Add item to the new link list // ------------------------------------------------------------ copyList.Add (start.Item); start = start.Previous; } linkedList = copyList; // ------------------------------------------------------------ // That's it! // ------------------------------------------------------------ }

1

Utilisateur anonyme

24 déc. 2015

You program is taking extra space..... Der are other good which does not require extra space

2