We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Reverse a linked list
Reverse a linked list
Sort by
recency
|
918 Discussions
|
Please Login in order to post a comment
For Python3 Platform
Here is Reverse a linked list solution in python, java, c++ and c programming - https://programmingoneonone.com/hackerrank-reverse-a-linked-list-problem-solution.html
MY JAVA SOLUTION: SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) { SinglyLinkedListNode*temp=llist; SinglyLinkedListNode*newnode; int count=0; while(temp!=NULL){ count+=1; temp=temp->next; } int arr[count]; int i=0; while(llist!=NULL){ arr[i]=llist->data; llist=llist->next; i+=1; } for(int j=count-1;j>-1;j--){ newnode=(struct SinglyLinkedListNode*)malloc(sizeof(struct SinglyLinkedListNode*)); newnode->data=arr[j]; newnode->next=NULL; if(llist==NULL){ llist=newnode; temp=llist; } else{ temp->next=newnode; temp=newnode; } } return llist;
}
function reverse(llist) { var node = null; while(llist != null) { var temp = node; node = new SinglyLinkedListNode(llist.data,temp) node.next = temp llist = llist.next } return node
}
javascript solution:
` function reverse(llist) { let prev, next = null;
} `