1

Why is it than we can use singly-linked list to represent a stack only if the links between nodes are oriented to point in the direction of top (newer) to bottom (older)?

2 Answers 2

2

Because otherwise operations like pop() will take O(n) instead of O(1) and same with the other ones. To represent a stack means accessing the last inserted item is the easiest (less time).

Sign up to request clarification or add additional context in comments.

Comments

-1

Okay i get it now, removing the head of a singly linked list take O(1) time and removing the tail takes O(n) time since you have to do a linear search to find the node that points to the tail and make that node point to null. So if the stack was tail to head (each pointer points up towards top of stack) it would take O(n) every time you popped as opposed to O(1) for a stack thats implemented head to tail (each pointer points to bottom of stack).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.