Amazon Interview Report
- 0of 0 votes
AnswersReverse K elements of a linked list.
Solved on 31st May.
It still has one bug, if the number of elements in a linked list are not a multiple of K, then it truncates the linked list at a multiple of K.
Eg. if K=5 and number of elements is 12, it will reverse till 10 th element and then truncate it. so when the reversed list is printed, only 10 elemtns with ever 5 revesed is shown, need to fix this.
- chaos May 23, 2012 in United Statesimport java.util.*; class link { int data; public link nextlink; link(int d1) { data = d1; }} class List{ link head; static int rev; link revhead; List(){ head = null; Scanner scan1 = new Scanner(System.in); rev = scan1.nextInt(); } boolean isEmpty(link head) { return head==null; } void insert(int d1) { link templink = new link(d1); templink.nextlink = head; head = templink; } void printlist(){ link head1 = head; while(!isEmpty(head1)) { System.out.print(head1.data + " "); head1 = head1.nextlink; } System.out.println(); } void reverse() { boolean flag=true; revhead = head; link previous=revhead,temp=null,temp2=null;int i=1; while(true) { i++; for(int j=0;j<(2*rev-1);j++) { if(isEmpty(previous)) { // previous=temp2; break; } temp2=previous; previous=previous.nextlink; } for(int k=0;k<rev;k++) { if(isEmpty(revhead)) { return; } temp = revhead.nextlink; revhead.nextlink = previous; previous = revhead; revhead = temp; } if(flag){ head=previous; flag=false; } previous=temp; } } } class RevKLinkedList { public static void main(String[] args) { List list1 = new List(); list1.insert(10); list1.insert(20); list1.insert(30); list1.insert(40); list1.insert(50); list1.insert(60); list1.insert(70); list1.insert(80); list1.insert(90); list1.insert(100); list1.insert(110); list1.insert(120); list1.printlist(); list1.reverse(); list1.printlist(); } }
| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm - 0of 0 votes
AnswersReverse K elements of a linked list.
Solved on 31st May.
It still has one bug, if the number of elements in a linked list are not a multiple of K, then it truncates the linked list at a multiple of K.
1->2->3->4->5->6 becomes
3->2->1->6->5->4 for K=3
Eg. if K=5 and number of elements is 12, it will reverse till 10 th element and then truncate it. so when the reversed list is printed, only 10 elemtns with every 5 revesed is shown, need to fix this.
- chaos May 30, 2012 in United Statespackage ThreadPkg; import java.util.*; class link { int data; public link nextlink; link(int d1) { data = d1; }} class List{ link head; static int rev; link revhead; List(){ head = null; Scanner scan1 = new Scanner(System.in); rev = scan1.nextInt(); } boolean isEmpty(link head) { return head==null; } void insert(int d1) { link templink = new link(d1); templink.nextlink = head; head = templink; } void printlist(){ link head1 = head; while(!isEmpty(head1)) { System.out.print(head1.data + " "); head1 = head1.nextlink; } System.out.println(); } void reverse() { boolean flag=true; revhead = head; link previous=revhead,temp=null,temp2=null;int i=1; while(true) { i++; for(int j=0;j<(2*rev-1);j++) { if(isEmpty(previous)) { // previous=temp2; break; } temp2=previous; previous=previous.nextlink; } for(int k=0;k<rev;k++) { if(isEmpty(revhead)) { return; } temp = revhead.nextlink; revhead.nextlink = previous; previous = revhead; revhead = temp; } if(flag){ head=previous; flag=false; } previous=temp; } } } class RevKLinkedList { public static void main(String[] args) { List list1 = new List(); list1.insert(10); list1.insert(20); list1.insert(30); list1.insert(40); list1.insert(50); list1.insert(60); list1.insert(70); list1.insert(80); list1.insert(90); list1.insert(100); list1.insert(110); list1.insert(120); list1.printlist(); list1.reverse(); list1.printlist(); } }
| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm