Problem Statement:
Fold the given Linked List
Solution:
Download Source Code
As a Scarf is folded , the above linked list is also folded and merged.
The recursive Logic in-order to achieve this is given below. I would recommend you to
try it yourself and also refer to Check Pallindrome List , which involves similar logic.
Please comment and post your suggestions , any suggestion for enhancing the solution is most-welcome.
Happy Coding !! :)
Fold the given Linked List
Solution:
Download Source Code
As a Scarf is folded , the above linked list is also folded and merged.
The recursive Logic in-order to achieve this is given below. I would recommend you to
try it yourself and also refer to Check Pallindrome List , which involves similar logic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public static Node foldList(Node head) { return fold(head, head); } /** * Linked List Folding Sub-routine * @param head: head pointer * @param curr: current pointer * @return: head of the folded list */ private static Node fold(Node head, Node curr) { if (curr == null) return head; Node result = fold(head, curr.next); // condition to stop execution if result is head and curr is not the // last node if (result != head || curr.next == null) { // handling odd and even number of nodes if (result != curr && result.next!=curr) { curr.next = result.next; result.next = curr; return curr.next; } curr.next = null; } return head; } |
Please comment and post your suggestions , any suggestion for enhancing the solution is most-welcome.
Happy Coding !! :)
Hello Prateek
ReplyDeleteThe Algorithm mentioned above fails when number of node in input list is Even...
Providing a recursive alternative Algorithm
private Node lastNode(Node node){
Node prev = null;
if(node.next == null){
return null;
}
while(node.next != null){
prev = node;
node = node.next;
}
prev.next = null;
return node;
}
public Node foldLinkList(Node node){
if(node != null){
Node lastNode = lastNode(node);
Node returnNode = foldLinkList(node.next);
if(returnNode!=null || lastNode!=null){
node.next = lastNode;
lastNode.next = returnNode;
}else{
node.next = returnNode;
}
return node;
}else{
return node;
}
}
Hi Ashis,
DeleteThanks for investing your time to find that flaw for even number of nodes.
I have updated the code.
I think you can work on the code that you have provided
1. Complexity is O(n^2)
2. number of lines of code can be reduced.
Hello Prateek
ReplyDeleteThe revised algo look good...
cheers
From small, mom and pop shops every one of the way through to heavily established market
ReplyDeleteleaders, each year businesses in every industry
and also every size decide to participate over these important
promotional events. Or you might simply have information on a related field that the target audience would find interesting.
In short, it's crucial for organizations to make certain they deliver enough exciting details to pique the crowd's interest without crossing the queue into the dreaded information overload territory.
Also visit my blog post ... inexpensive pop up booth displays
Some birds, for instance will take part in singing displays that mirror the motions from the grass display much like the Cutthroat finch.
ReplyDeleteIf you already possess a trade show booth designed,
understand it ready for the full analysis. Visit your customers' Facebook pages
and personally invite these phones stop by and visit your trade
exhibition displays and banner stands.
Here is my web site; inexpensive exhibits displays
Some birds, for example will take part in singing
ReplyDeletedisplays that mirror the motions of the grass display such as the
Cutthroat finch. To my thoughts there is nothing more standard or spontaneous than arranging fresh flowers
in the vase. Also, checking in with the business running the wedding can provide additional information on whom to use and whether you will find discounts provided to participating businesses.
Here is my web page - modular trade show display
Some birds, for instance will embark on singing displays that mirror the motions of the grass
ReplyDeletedisplay much like the Cutthroat finch. If you already
possess a display booth designed, get it ready for a
full analysis. This is caused by a number of elements which
feature strongly during these LED signs.
Look into my web page: buy velcro display panels
Hmm is anyone else having problems with the
ReplyDeletepictures on this blog loading? I'm trying
to find out if its a problem on my end or if it's the blog.
Any responses would be greatly appreciated.
my site :: blog content
Hello there! Quick question that's entirely off topic. Do you know how to
ReplyDeletemake your site mobile friendly? My blog looks weird when browsing from my iphone4.
I'm trying to find a template or plugin that might be able
to correct this problem. If you have any recommendations, please share.
Many thanks!
my blog post Las Vegas Kitchen Appliance Repair
I every time spent my half an hour to read this webpage's articles everyday along with a mug of coffee.
ReplyDeletemy website - hire a private jet prices
Hi there! I simply want to offer you a big thumbs up for the excellent
ReplyDeleteinformation you have right here on this post. I will be coming back to your
blog for more soon.
My web page; best wrinkle eye cream
What's up to all, it's actually a fastidious for me to visit this web
ReplyDeletesite, it contains helpful Information.
Also visit my web-site - private jet london
Hi, i think that i saw you visited my blog so i came to “return the favor”.I'm attempting to find
ReplyDeletethings to enhance my website!I suppose its ok to use some of your ideas!!
Feel free to visit my web-site: charter flight cost
Great blog! Is your theme custom made or did you download it from somewhere?
ReplyDeleteA design like yours with a few simple tweeks would really make my blog shine.
Please let me know where you got your theme.
Thanks a lot
my webpage: google search engine
moncler outlet
ReplyDeletevans shoes
kate spade outlet
kd 10
hermes handbags
coach outlet
christian louboutin outlet
coach outlet store
louboutin shoes
hermes online