Program took more time than expected error - java

Below is a program to reverse a doubly linked list in gfg.
When I try to submit, I get the following error
Your program took more time than expected. Time Limit Exceeded
Expected Time Limit 0.00sec.
Can anyone tell me how to decrease my program's time?
Code
public static Node reverseDLL(Node head)
{
Node p,cur;
p=head;
cur=head.next;
while(cur!=null)
{
if(cur.next==null)
{
p.next=cur.next;
cur.next=head;
head.prev=cur;
head=cur;
break;
}
p.next=cur.next;
cur.next.prev=p;
cur.next=head;
head.prev=cur;
head=cur;
cur=p.next;
}
return head;
}

Your code is not correct:
The prev member of the (original) tail node is not set correctly. It is not set in the if block, but in the previous iteration, where it got its value with cur.next.prev=p, which is setting it to the (original) head node. This creates an infinite loop in the data structure. In the end, none of the prev links is null. It might be that the testing framework keeps following those prev links in circles until a time out happens.
Also, the function assumes that head is not null. This may not be guaranteed.
There are also too many assignments happening. With a doubly linked list it is quite simple: just swap the value of next and prev in every node:
public static Node reverseDLL(Node head)
{
if (head == null) {
return null;
}
Node next = head.next;
while (next != null) {
head.next = head.prev;
head.prev = next;
head = next;
next = head.next;
}
head.next = head.prev;
head.prev = null;
return head;
}

this is classic solution for O(N) time, so I guess time constraint for this task is required to. solve it for O(1).
Take a look for this thread: https://www.google.com/url?sa=t&source=web&rct=j&url=https://discuss.codechef.com/t/reverse-a-doubly-linked-list-in-o-1/72850&ved=2ahUKEwjipdrQw_T1AhVYLTQIHahIDx8QFnoECBQQAQ&usg=AOvVaw1c0BDUotM0suEK7I4B9pQs

Related

Remov n-th node from the end of list [LeetCode]

The question is very straight forward and I got the algorithm as well. The only thing I don't understand is the return statement. why do we need to return dummy.next, when we haven’t make any change to dummy at all.
I guess my question is when we initially set slow and fast equal to dummy, aren't we just making a deep copy of the list/dummy (meaning whatever change we made in either of them does not affect dummy), or am i missing something..?
Any help would be appreciated. if you can back up your explanation with an example, would be awesome.
And here is link to the question: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode slow = dummy, fast = dummy;
for(int i = 0; i <= n ; i++){
fast = fast.next;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
The only difference that I can see between using the dummy and not would be in the case of having to remove the first element in the list. In that case, without using the dummy there would not be a way to remove the head node. However, this can easily be fixed by adding a simple check during the initial loop.
Here is my solution, omitting the use of the dummy head.
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode slow = head, fast = head;
for(int i = 0; i <= n ; i++){
if (fast == null) {
return head.next;
}
fast = fast.next;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
To answer your other questions,
aren't we just making a deep of the list/dummy
there is no deep copying going on since the dummy keeps a reference of the head, not a copy.
why do we need return dummy.next, when we haven’t make any change to dummy at all.
the dummy is defined to be before the head. so when we want to return the changed head, we return the next node after dummy. If we just returned dummy, then the answer that we would get would have an extra 0 in the front of it, which we don't want.
To answer your questions,
when we initially set slow and fast equal to dummy, aren't we just
making a deep of the list/dummy
No it doesn't. The assignment is by reference as ListNode is a reference type. Thus changing any of dummy, slow, fast will show its affect on all three variables.
the only thing I don't understand is the return statement. why do we
need return dummy.next
As for the return statement, we still need to return the start of list that was provided to us as a parameter head in function, which is being pointed by dummy.next
As for why is the operations directly not performed on head and there is need of dummy variable and steps
ListNode dummy = new ListNode(0);
dummy.next = head;
the reason might be cover up all edge scenarios. eg : Linked List contains only one node and it is removed or the first node of linked list is removed.

Java Queue Implementation with Pointers

I need help understanding this. I know how to implement the queue but there is one small part bothering me. I drew on my notebook the flow of how things work but I don't get how the head has a nextNode without me setting it. How does the head end up pointing to the next node?
public void enqueue(T data){
count++;
Node<T> current = tail;
tail = new Node<>(data);
if(isEmpty()) {
///////////////////////////////////////////////////////////////////////////////
// when this runs, doesn't head.getNextNode point to null?
// if the list is empty, then tail is null.
// On the deque method, I can sout head.getNextNode() and I get data back, how?
///////////////////////////////////////////////////////////////////////////////
head = tail;
} else {
current.setNextNode(tail);
}
}
Below, the dequeing works fine, I think I'm having an issue understanding the whole reference/pointer thing
public T dequeue() {
if(isEmpty()) {
return null;
}
count--;
T dataToRemove = head.getData();
/////////////////////-+{[UPDATE]}+-////////////////////////////////
// WHERE DOES HEAD GET THE NEXT NODE FROM? THIS WORKS, BUT WHERE IS
// THE NEXT NODE COMING FROM IS WHAT I'M ASKING?
///////////////////////////////////////////////////////////////////
head = head.getNextNode();
return dataToRemove;
}
I figured it out:
When the list is empty, the head points to tail
then, when the enqueue method gets called again,
current will be pointing to tail, but head will still be pointing to tail reference
so now head is pointing to the same reference as current
in the else statement, current sets next node to the same reference the
head is pointing to. That's how the head gets its nextNode set.
When the method runs again, current will
point to another reference again but head will still be pointing to its original reference. BAM
public void enqueue(T data){
count++;
Node<T> current = tail;
tail = new Node<>(data);
if(isEmpty()) {
head = tail;
} else {
current.setNextNode(tail);
}
}

Circular Linked List Node Insertion Java

I tried implementing the insert method for circular linked list. I think I had some success.
Problem:
When I display the list. The display method will loop because every next variable of the link is linked to a non-null node object. So head will never be a null object. From what I recall about singly linked list, head always point to the first node in the list or the first node with data inside of it.
My conceptual understanding of circular linked list:
From what I can understand circular linked is somewhat like a singly linked list but with a slight twist: the next variable of the tail object points to the head.
I'm coding it like the diagram has presented provided by the source link.
Source: http://sourcecodemania.com/circular-linked-lists/
public void insert(String data)
{
Link link = new Link(data);
if(head == null)
{
head = link;
tail= link;
}
else
{
tail.next = link;
tail = link;
tail.next = head;
}
}
public void display()
{
// good implementation for display #2
while(head != null)
{
// System.out.println (head.data);
head = head.next;
}
}
Once you insert at least one element, you would never come across null. It will keep on going till infinity.
Also, it might not be a good idea to modify head just for displaying the list. Operation like display should not have any side effects.
In stead, keep a member field size in your list class and update it in each insert and delete method.
Now you would know how many times you should iterate the loop.
ListClassName current = head; // Head is not modified.
for (int i = 0; i < this.size; i++) {
// System.out.println (current.data);
current = current.next;
}
Good luck.
You can keep a reference to the first Link object and check to make sure head is not equal to this object while looping:
public void display()
{
boolean first=true;
Link firstItem=null;
// good implementation for display #2
while(head != null && head!= firstItem)
{
if(first){
firstItem=head;
first=false;
}
// System.out.println (head.data);
head = head.next;
}
}

LinkedList remove(), next, prev and head

I need to remove() from a (custom) LinkedList, setting last iterated item to null. There are two cases, one where prev is head, one where it is not. The nodes are next, prev and head. I was wondering if you spot any problems in the following code? Do I need to set the iterator variables in between? (or will emptying prev do the trick?) Thanks guys!
public void remove() {
if(prev == null) {
throw new IllegalStateException();
}
else {
if(prev == head){
head = head.next;
prev = null;
sizeOfList--;
}
else {
prev = null;
sizeOfList--;
}
}
}
In case your previous is not the head(second case in your code) you should fix the next link of the previous node before prev. Otherwise it will be left pointing to prev.

LinkedList remove conditions (last iterated item)

I am attempting to remove the last iterated element, with a custom iterator/linked list class. It for some reason only does this for the first item in the list (the head condition). Is there anything wrong with the conditions?
Should I, instead of the Else after If (prev=head), write If (next != null) to find middle nodes, and If (next = null) to find the last node?
Second question: to remove the items, should I also write prev.element = null (now I only have prev = null, and I suppose that erases the node but not its content.
Quite simply, what is wrong with my remove method, as I cannot figure it out myself. Thank you guys so much in advance. I have been working many hours with this but I still haven't got it working.
public E next() {
if (!hasNext())
throw new NoSuchElementException ();
prev = next;
E element = next.element;
next = next.next;
return element;
}
public void remove() {
if(prev == null) {
throw new IllegalStateException();
}
else {
if(prev == head){
head = head.next;
next = head;
}
else {
next = prev.next;
}
sizeOfList--;
prev = null;
}
}
You would need a while loop to be able to go through every node in the list until you hit the last one. As it is now, your code simply goes past the head, and then gets into the code that says sizeOfList-- and then prev = null;
You need something like this:
while (prev.next.next != null) {
prev = prev.next;
}
prev.next = null;
I do prev.next.next so that you can set the 2nd to last node in your linked list to point to a null value (which is done by prev.next = null;). Think of it this way: prev is the 2nd to last element in the list, prev.next is the last element, and obviously prev.next.next HAS to be null (because prev.next is LAST.) So once this is the case, delete the last element by setting the 2nd to last element to point to a null value.
And then decrement your list count.
This is my best guess with the given code
if(prev == head){ should change to if(prev.equals(head)){ Use equals method.
And I think you have to override equals method in the corresponding element class might definitely help.
== only checks for whether both variables refer to same object in memory, where as equals check Object state.
I hope it helps :).

Categories

Resources