## Chegg.com Interview Question for Consultants

Comment hidden because of low score. Click to expand.
1
of 1 vote

``````public class ReverseLinkedList {

Node prev = null;
while (current.next != null) {
temp = current.next;
current.next = prev;
prev = current;
current = temp;
}
current.next = prev;
return current;
}``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse the list by iteratively swapping pointers, then traverse it outputting as you go.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````node* reverse(node* root)
{
node* p,q,r,s;
p = root;
q = p;
r = NULL;
while(q!=NULL)
{
s = r;
r = q;
q = q->next;
r->next = s;
}
return(q);
}

int main()
{
......
reverse(root);
node*q =root;
while(q!=NULL)
{
printf("%d",q->data);
q = q->next;``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

1. reverse the linked list, using three pointers
2. traverse the reversed linked list
linear time algorithm, constant additional memory

Comment hidden because of low score. Click to expand.
0
of 0 vote

@ gekko
look at the first response!

why do people like repeating things?
Jesus help the poor fellow..

Comment hidden because of low score. Click to expand.
0
of 0 vote

hi see this code,

``````node* rev(node *p)//initally p is pointing to starting node
{
if(p->next)
{
rev(p->next)->next=p;
p->next=NULL;
return p;
}
else
{
return p;
}
}``````

Comment hidden because of low score. Click to expand.
0

No recursion

Comment hidden because of low score. Click to expand.
0
of 0 vote

when you reverse and then traverse the reversed list, you can restore it to the original pointer. So your original list is unharmed because of this reverse output (which is supposed to be read-only).

Comment hidden because of low score. Click to expand.
0

1. Reverse the list
2. Print the list in this order
3. Reverse the list again ?

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class ReverseLinkedListRecursion {

}

public Node reverse(Node temp, Node current, Node prev) {
if (current.next == null) {
current.next = prev;
return current;
} else {
Node t = current.next;
current.next = prev;
return reverse(t, temp, current);
}``````

}}

Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's a quick JS version

``````function reverse(arr) {
const swap = (a, first, last) => {
const temp = a[first];
a[first] = a[last]
a[last] = temp;
}

for (var i = 0; i < arr.length - 1; i++) {
console.log(arr[i], arr[(arr.length - 1) - i]);
swap(arr, i, (arr.length - 1) - i);
}
return arr;
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

### Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

### Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.