jie.jeff.li
BAN USER// Recursive code
Node Reverse(Node current, Node pre)
{
Node next=current.next;
current.next=pre;
if(next==null)
return current;
return Reverse(next,current);
}
Node Reverse(Node head)
{
if(head==null)
return null;
return Reverse(head,null);
}
//Iterative code
Node ReverseIterative(Node head)
{
if(head==null)
return null;
Node pre=null;
Node current=head;
while(current!=null)
{
Node next=current.next;
current.next=pre;
pre=current;
current=next;
}
return pre;
}
How about this:
Node* reverseLinkList(Node **head)
{
Node *pre=null;
Node *next=null;
while(*head!=null)
{
next=*head->next;
*head->next=pre;
pre=*head;
*head=next;
}
return *head;
}
get the length of the number
- jie.jeff.li April 23, 2014First get the smallest and biggest one lucky numbers in the length of the number.
if the number is smaller than the smallest one, return smallest one
if the number is bigger or equal to the biggest one, return the smallest lucky number of length+1
else the number is between smallest and biggest lucky numbers, we can get all the lucky numbers in the length, then go through all the lucky numbers, find the first lucky number that is bigger than the number.