Question

ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in reverse order when you are NOT allowed to al

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a) Node reverseWIthoutDynamic(Node node) {
   Node prev = null;
   Node current = node;
   Node next = null;
   while (current != null) {
   next = current.next;
   current.next = prev;
   prev = current;
   current = next;
   }
   node = prev;
   return node;
   }
   // Time complexity = O(N)
(b) Node reverseWIthDynamic(Node node) {
   Node newList = null;
   Node current = node;
  
   while (current != null) {
   Node newnode = new Node(current.data);
   newnode.next = newList;
   newList = newnode;
   }
  
   return newList;
   }
   // Time complexity = O(N)

(c)

We can find minimum element in O(Logn) using Binary Search.

The minimum element is the only element whose previous is greater than it. We check this condition for middle element by comparing it with (mid-1)’th and (mid+1)’th elements.
If minimum element is not at middle (neither mid nor mid + 1), then minimum element lies in either left half or right half.
If middle element is smaller than last element, then the minimum element lies in left half.
Else minimum element lies in right half.

Add a comment
Know the answer?
Add Answer to:
ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT