Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Got it, the whilecondition should be while(!isEmpty(head)), the '!' was missing.
Also need to change head to point to the new head. So after the loop finishes, I need to add
head=previous; the new reverse function would be
void reverse()
{
revhead = head;
link previous=null,temp=null;
while(!isEmpty(revhead))
{
temp = revhead.nextlink;
revhead.nextlink = previous;
previous = revhead;
revhead = temp;
}
//To make head point to the new head, which was the last element earlier
//but now after reversing is the head
head = previous;
}
def reverse(node):
curr = node
prev = None
while curr is not None:
next = curr.next
curr.next = prev
prev = curr
curr = next
def reverse_every_k(node, k):
curr = node
prev = None
i = 0
while curr is not None:
next = curr.next
if i != 0 and i % k == 0:
curr.next = prev
prev = curr
curr = next
Missing things...
1. increament of i
2. i should be initialized to 1
3. check if k ==0 ...before while....
#!/usr/bin/python
class MyList:
class ListNode:
def __init__(self,value):
self.value=value;
self.next = None;
def __init__(self):
self.head=None;
def append(self,value):
node = self.ListNode(value)
if (self.head==None):
self.head = node
else:
node.next = self.head
self.head = node
def reverse(self):
cursor = self.head
self.head = self.head.next
cursor.next = None
while self.head.next:
temp = self.head.next
self.head.next = cursor
cursor = self.head
self.head = temp
self.head.next = cursor
def __str__(self):
cursor = self.head
string = '( '
while cursor:
string += "{} ".format(cursor.value)
cursor=cursor.next
string += ')'
return string
list = MyList()
list.append(1)
list.append(2)
list.append(3)
list.append(4)
list.append(5)
print list
list.reverse()
print list
I am going to explain the basic concept, coding it should be easy, to reverse a list such as
1->2->3->4.
Curr=Head;
Last=Head;
1).Get a pointer to the last element, by traversing till the end
2). While(Curr!=Last)
4). temp = curr;
5). If Last.Next!=null, than temp.next=Last.Next;
6).Last.Next = Temp;
7).End While
8)Head=Last;
1-2-3-4
Last = 4;
2-3-4-1
3-4-2-1
4-3-2-1
Head = 4
void kreverse(int len)
{
node *st=start,*p=start,*q=p->next,*r=q->next;
int i=0;
while(i<len)
{
i++;
q->next=p;
p=q;
q=r;
r=r->next;
}
st->next=q;
start=p;
}
struct node * reverse(struct node *prev,struct node * curr,struct node *next)
{
if(curr!=NULL)
{
curr->link=prev;
prev=curr;
curr=next;
if(next==NULL)
return prev;
else
{
next=next->link;
reverse(prev,curr,next);
}
}
}
I don't understand why its not working.
i tried a lot but could not find the bug . i am reversing a list in blocks of nodes.
here is my code:
int hasnode(struct node *s,int k) //checks if k node exist
{
int i;
for(i=1;s&&i<k;i++)
s=s->next;
if(i==k)
return 1;
else
return 0;
}
struct node* getkplusoneth_node(struct node **nextnode,int k) //returns k+1th node
{
int i;
for(i=0;*nextnode&&i<k;i++)
*nextnode=*nextnode->next;
if(i==k)
return *nextnode;
}
void reverseinkblocks(int k) //reverse function
{
int i;
struct node* prev=NULL,*current=head,*nextnode=current,*temp;
while(current&&hasnode(current,k))
{
*nextnode=getkplusoneth_node(&nextnode,k);
while(current!=nextnode) //reverse upto one node before nextnode
{
temp=current->next;
current->next=prev;
prev=current;
current=temp;
}
}
}
void reverse_diffn(dnode **dpnode)
{
dnode *curnode = *dpnode;
unsigned int prevaddr = NULL, curraddr = NULL;
while (curnode != NULL)
{
curraddr = (unsigned int)curnode;
curnode = curnode->down;
*((unsigned int *)curraddr + 1) = (prevaddr);
prevaddr = curraddr;
}
*dpnode = ((dnode *)curraddr);
}
- Test May 18, 2012