Chegg.com Interview Question for Consultants






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

public class ReverseLinkedList {

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

}

- jasmine August 11, 2015 | Flag Reply
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.

- DArendash August 19, 2009 | Flag Reply
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;

}

- amit August 19, 2009 | Flag Reply
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

- Gordon Gekko August 19, 2009 | Flag Reply
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..

- googler August 20, 2009 | Flag Reply
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
        {
          head=p;
          return p;
        }
    }

- newcomer August 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No recursion

- hopebasedcoder August 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

addendum:
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).

- Anonymous February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- abhimanipal April 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseLinkedListRecursion {

	public Node reverse(Node head) {
		return reverse(head, head, null);
	}

	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);
		}

}}

- jasmine August 11, 2015 | Flag Reply
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;
}

- pc May 06, 2019 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

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.

Learn More

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.

Learn More