create an singly linked lists and reverse the lists by
interchanging the links and not the data?

Answers were Sorted based on User's Feedback



create an singly linked lists and reverse the lists by interchanging the links and not the data?..

Answer / guest

public class ReverseList {

public static void main(String[] args) {
ReverseList revalgo = new ReverseList ();
Node n1 = new Node();
n1.data = "A";
revalgo.insert(n1);

n1 = new Node();
n1.data = "B";
revalgo.insert(n1);

n1 = new Node();
n1.data = "C";
revalgo.insert(n1);

n1 = new Node();
n1.data = "D";
revalgo.insert(n1);

n1 = new Node();
n1.data = "E";
revalgo.insert(n1);
System.out.println("Link List");
revalgo.print();
System.out.println("Reversed Link List");
revalgo.reverse();
revalgo.print();
}

void insert(Node n)
{
n.next = root;
root = n;
}

void print()
{
Node current = root;
while(current != null)
{
System.out.print(current.data + "->");
current = current.next;
if(current == null)
System.out.println(""+ null);
}
}

void reverse()
{
Node current = null;
Node prev = null;
while(root != null)
{
current = root;
root = root.next;
current.next = prev;
prev = current;
}
root = current;
}

Node root = null;
}

class Node {
String data;
Node next;
}

Is This Answer Correct ?    1 Yes 2 No

create an singly linked lists and reverse the lists by interchanging the links and not the data?..

Answer / vaishali naidu

We can achive this using following method:
Use three pointers
First is start pointing to first node.
Second is prev pointing to second node
Third is curr pointing to third node.
a.while(start!=curr)
{
prev->next=start
start->next=NULL;
start=prev;
prev=curr;
curr=curr->next;
}

Is This Answer Correct ?    13 Yes 21 No

create an singly linked lists and reverse the lists by interchanging the links and not the data?..

Answer / sanjay rajput

struct node
{
int val;
struct node *next;
};

typedef struct node NODE;
NODE *p,*start;
p=start->next;
while(p!=NULL)
{
p->next=start;
start=p;
p=p->next;
}

Is This Answer Correct ?    1 Yes 10 No

Post New Answer

More Data Structures Interview Questions

Advanced problems related to Data Structures were asked

0 Answers   Motorola,


What is quick sort example?

0 Answers  


What is a queue in data structure?

0 Answers  


Which is better hashmap or hashtable?

0 Answers  


Which is faster array or list?

0 Answers  






what is binary tree?

12 Answers   BMC,


Can we insert null in set?

0 Answers  


What is a spanning tree?does the minimum spanning tree of a graph give the shortest distance between any 2 specified nodes?

0 Answers  


What does the term sorting refer to?

0 Answers  


Two linked lists are given, find out the sum of them without altering the linked list?

0 Answers   Expedia,


Are sets sorted?

0 Answers  


If I try to add enum constants to a treeset, what sorting order will it use?

0 Answers  


Categories