Morgan Stanley Interview Question
Software Engineer / Developerspublic void reverse() {
List<String> single = new LinkedList<String>();
List<String> reverse = new LinkedList<String>();
single.addAll(Arrays.asList("a","b","c","d"));
for (String s : single) {
System.out.println(s);
}
for (int i=single.size()-1;i>=0;i--) {
reverse.add(single.get(i));
}
for (String s : reverse) {
System.out.println(s);
}
}
node* reverse(node* head){
node* pre=null, cur=head;
while(cur!=null){
cur->next = pre;
pre = cur;
cur = cur->next;
}
return pre;
}
static void reverse(struct node **head){
struct node *result = null;
struct node *current = *head;
struct node *next;
while(current != null){
next = current->next;
current->next = result; // first time it is null
result = current;
current = next;
}
*head = result; //get the pointer to point to the new head
}
package com.test.again;
public class LinkedList {
public static void main(String[] args) {
Node root = buildLinkedList();
Node reverse = reverseLinkedList(root);
System.out.println(reverse);
}
private static Node buildLinkedList() {
Node n1 = new NodeImpl();
n1.setData(10);
Node n2 = new NodeImpl();
n2.setData(8);
Node n3 = new NodeImpl();
n3.setData(12);
Node n4 = new NodeImpl();
n4.setData(14);
n1.setNext(n2);
n2.setNext(n3);
n3.setNext(n4);
return n1;
}
public static Node reverseLinkedList(Node root) {
if(root == null) return null;
Node rest = root.getNext();
Node temp = null;
while ( rest != null ){
temp = rest.getNext();
rest.setNext(root);
root = rest;
rest = temp;
}
return root;
}
}
interface Node {
Node getNext();
void setNext(Node n);
int getData();
void setData(int data);
}
class NodeImpl implements Node {
private int data;
private Node next;
@Override
public Node getNext() {
return next;
}
@Override
public void setNext(Node n) {
this.next = n;
}
@Override
public int getData() {
return data;
}
@Override
public void setData(int data) {
this.data = data;
}
}
Java
Node reverse(Node head) {
Node previous;
Node current;
Node next;
if (head == null || head.next == null) {
return head;
}
previous = null;
current = head;
next = head.next;
while (current != null) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}
return previous;
}
Recursive algorithm:
public class ListTask {
private static class Node {
Node nextNode;
int value;
private Node(Node nextNode, int value) {
this.nextNode = nextNode;
this.value = value;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public static void main(String[] args) {
Node last = new Node(null, 3);
Node nextNode = new Node(last, 2);
Node first = new Node(nextNode, 1);
Node node = reverseList(first, first.getNextNode());
}
private static Node reverseList(Node previous, Node nextNode) {
if (nextNode == null) {
return previous;
}
previous.setNextNode(null);
Node node = reverseList(nextNode, nextNode.getNextNode());
nextNode.setNextNode(previous);
return node;
}
}
my favorite one:
- edward March 02, 2011NODE* revlist(NODE *head)
{
NODE *tmp=NULL, *tmp1=NULL;
while (head != NULL)
{
tmp = head;
head = head->next;
tmp->next = tmp1;
tmp1 = tmp;
}
return head;
}