Oracle Interview Question for Software Engineer / Developers


Country: -
Interview Type: In-Person




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

A recursive implementation, call reverseList(head,null) to reverse the list:

public static<T> ListNode<T> reverseList(ListNode<T> head, ListNode<T> prev){
		if (head==null){return prev;}
		
		ListNode<T> temp = head.next();
		head.setNext(prev);
		// the head became the previous element and temp became the new head
		return reverseList(temp,head);
	}

- IvgenyNovo January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static LNode reverse(LNode head){
		if(head == null || head.next == null) return head;
		if (head.next.next == null){
			LNode tmp = head.next;
			tmp.next = head;
			head.next = null;
			head = tmp;
			
		}else{
			LNode previus = head;
			LNode current = previus.next;
			LNode next = current.next;
			previus.next = null;
			while(next != null){
				current.next = previus;
				previus = current;
				current = next;
				next = next.next;
	                }

	                current.next = previus;
	                head = current;
		}
		return head;
	}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
    struct node* prev   = NULL;
    struct node* current = *head_ref;
    struct node* next;
    while (current != NULL)
    {
        next  = current->next;  
        current->next = prev;   
        prev = current;
        current = next;
    }
    *head_ref = prev;
}

- Nit February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void recursiveReverse(struct node** head_ref)
{
    struct node* first;
    struct node* rest;
      
    /* empty list */
    if (*head_ref == NULL)
       return;   
 
    /* suppose first = {1, 2, 3}, rest = {2, 3} */
    first = *head_ref;  
    rest  = first->next;
 
    /* List has only one node */
    if (rest == NULL)
       return;   
 
    /* reverse the rest list and put the first element at the end */
    recursiveReverse(&rest);
    first->next->next  = first;  
     
    /* tricky step -- see the diagram */
    first->next  = NULL;          
 
    /* fix the head pointer */
    *head_ref = rest;              
}

- Nit February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void setNext(ListNode node) {
	this.node = node;
	}
public void getNext() {
	return this.next;
	}

ListNode ReverseList(ListNode head) {
	ListNode temp = null,nextNode = null;
	while(head != null) {
		nextNode = head.getNext();
		head.setNext(temp);
		temp = head;
		head = nextNode;
	}
	return temp;
}

Time complexity:O(n) Space Complexity:O(1)

- Vaibhavs February 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given head, reverse rest of list and then add head at the end.

private static Node reverse(Node node) {
        if (node == null || node.next == null) {
            return node;
        }
        Node newHead = reverse(node.next);
        node.next.next = node;
        node.next = null;
        return newHead;
    }

- ajit@ajitk.in April 23, 2016 | 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