Domanda di colloquio di LinkedIn

Reverse a linked list

Risposte di colloquio

Anonimo

23 ago 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

Anonimo

24 dic 2015

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

2