Algorithms

Singly linked list is palindrome without extra space

<p>In this article&comma; we will discuss how to check a singly linked list is palindrome or not without using any extra space&period; As we know that a singly linked list has several nodes&comma; and each node in the list has the content and a pointer to the next node in the list&period; It does not store any pointer to the previous node&period; To store a single linked list&comma; only the pointer to the first node in that list must be stored&period; The last node in a single linked list points to nothing&period;<&sol;p>&NewLine;<h2><img class&equals;"aligncenter wp-image-4262 size-full" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2018&sol;09&sol;pallined&period;png" alt&equals;"singly linked list is palindrome" width&equals;"407" height&equals;"181" &sol;> A singly linked list is a palindrome<&sol;h2>&NewLine;<p>We have a singly linked list of numbers or characters&comma; we have check this singly linked list is a palindrome or not&comma; so we have to write a method that returns true if the given list is a palindrome&comma; else false&period;<&sol;p>&NewLine;<p>In the previous articles we have discussed many more algorithm related to the Linked list like the following&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;nth-node-from-the-end-of-a-singly-linked-list&sol;">Find the nth node from the end of a singly linked list<&sol;a><&sol;strong><&sol;li>&NewLine;<li><strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;find-the-middle-element-in-a-linked-list&sol;">Find the middle element in a linked list<&sol;a><&sol;strong><&sol;li>&NewLine;<li><strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;reverse-linked-list&sol;">How to Reverse linked list in Java<&sol;a><&sol;strong><&sol;li>&NewLine;<li><strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;delete-given-node-from-singly-linked-list&sol;">Delete given node from a singly linked list<&sol;a><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>We have a constraint for this algorithms as there is no extra space to be used&period; If we this constraint is not there then it is very simple to test like the following&colon;<&sol;p>&NewLine;<pre>public class Node &lbrace; &NewLine;&Tab; &NewLine;Node next&semi; &NewLine;String data&semi; &NewLine; &NewLine;public Node&lpar;String data&rpar; &lbrace; &NewLine; super&lpar;&rpar;&semi; &NewLine; this&period;data &equals; data&semi; &NewLine;&rcub; &NewLine;&period;&period;&period; &NewLine;&period;&period;&period; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3>Approach 1&colon; With Extra Space<&sol;h3>&NewLine;<p><strong>Step 1&colon;<&sol;strong> We can assign this the given linked list into another linked list<br &sol;>&NewLine;<strong>Step 2&colon;<&sol;strong> And reverse one of the linked lists&period;<br &sol;>&NewLine;<strong>Step 3&colon;<&sol;strong> Traverse both lists again and compare data of each node of both linked list&period;<br &sol;>&NewLine;<strong>Step 4&colon;<&sol;strong> If all nodes matched&comma; then return true&comma; else false&period;<&sol;p>&NewLine;<p>Let&&num;8217&semi;s see the code<&sol;p>&NewLine;<pre class&equals;"lang&colon;java decode&colon;true ">&sol;&ast;&ast; &NewLine; &ast; This program will check palindrome linked list with using extra space &NewLine; &ast;&sol; &NewLine;package com&period;dineshonjava&period;algo&semi; &NewLine; &NewLine;import java&period;util&period;Optional&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class LinkedListTest &lbrace; &NewLine; &NewLine;&Tab;&sol;&ast;&ast; &NewLine;&Tab; &ast; &commat;param args &NewLine;&Tab; &ast;&sol; &NewLine;&Tab;public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine;&Tab;&Tab; &NewLine;&Tab;&Tab;int arr&lbrack;&rsqb; &equals; &lbrace;1&comma; 2&comma; 3&comma; 2&comma; 1&rcub;&semi; &NewLine; Node head &equals; push&lpar;arr&rpar;&semi; &NewLine; &sol;&sol; printList&lpar;head&rpar;&semi; &NewLine; &NewLine; if &lpar;isPalindromicLinkedListWithExtraSpace&lpar;head&rpar;&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Is Palindrome"&rpar;&semi; &NewLine; &rcub; else &lbrace; &NewLine; System&period;out&period;println&lpar;"Not Palindrome"&rpar;&semi; &NewLine; &rcub; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;public static Node push&lpar;int arr&lbrack;&rsqb;&rpar; &lbrace; &NewLine;&Tab;&Tab; Node head &equals; new Node&lpar;String&period;valueOf&lpar;arr&lbrack;0&rsqb;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab; Node current &equals; head&semi; &NewLine;&Tab;&Tab; for &lpar;int i &equals; 1&semi; i&lt&semi; arr&period;length &semi; i&plus;&plus;&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab; Node newNode &equals; new Node&lpar;String&period;valueOf&lpar;arr&lbrack;i&rsqb;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab;&Tab; current&period;next &equals; newNode&semi; &NewLine;&Tab;&Tab;&Tab; current &equals; newNode&semi; &NewLine;&Tab;&Tab; &rcub; &NewLine;&Tab;&Tab; &Tab; &NewLine;&Tab;&Tab; return head&semi; &NewLine; &rcub; &NewLine;&Tab; &NewLine;&Tab;public static boolean isPalindromicLinkedListWithExtraSpace&lpar;Node head&rpar; &lbrace; &NewLine;&Tab;&Tab; &NewLine;&Tab;&Tab;if&lpar;head &equals;&equals; null &vert;&vert; head&period;next &equals;&equals; null&rpar; &NewLine;&Tab;&Tab;&Tab;return true&semi; &NewLine;&Tab;&Tab; &NewLine;&Tab;&Tab;Node second&lowbar;list &equals; head&semi; &NewLine;&Tab;&Tab;Node first&lowbar;list &equals; head&semi; &NewLine;&Tab;&Tab;printList&lpar;first&lowbar;list&rpar;&semi; &NewLine;&Tab;&Tab;second&lowbar;list &equals; reverseUsingIteration&lpar;second&lowbar;list&rpar;&semi; &NewLine;&Tab;&Tab;printList&lpar;second&lowbar;list&rpar;&semi; &NewLine;&Tab;&Tab;while &lpar;second&lowbar;list &excl;&equals; null &amp&semi;&amp&semi; first&lowbar;list &excl;&equals; null&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;if&lpar;Integer&period;valueOf&lpar;second&lowbar;list&period;data&rpar; &excl;&equals; Integer&period;valueOf&lpar;first&lowbar;list&period;data&rpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;&Tab;return false&semi; &NewLine;&Tab;&Tab;&Tab;&rcub; &NewLine;&Tab;&Tab;&Tab;second&lowbar;list &equals; second&lowbar;list&period;next&semi; &NewLine;&Tab;&Tab;&Tab;first&lowbar;list &equals; first&lowbar;list&period;next&semi; &NewLine;&Tab;&Tab;&rcub; &NewLine;&Tab;&Tab;return true&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;private static Node reverseUsingIteration&lpar;Node head&rpar;&Tab;&lbrace; &NewLine; if&lpar;head&period;getNext&lpar;&rpar; &equals;&equals; null&rpar; &lbrace; &NewLine; &Tab;return head&semi; &NewLine; &rcub; &NewLine; Node currentNode &equals; head&semi; &NewLine; Node nextNode &equals; null&semi; &NewLine; Node previousNode &equals; null&semi; &NewLine; &Tab;&Tab; &NewLine; while&lpar;currentNode &excl;&equals; null&rpar; &lbrace; &NewLine; &Tab;nextNode &equals; currentNode&period;getNext&lpar;&rpar;&semi; &NewLine; &Tab;currentNode&period;setNext&lpar;previousNode&rpar;&semi; &NewLine; &Tab;previousNode &equals; currentNode&semi; &NewLine; currentNode &equals; nextNode&semi; &NewLine; &rcub; &NewLine; return previousNode&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab; &NewLine;&Tab;public static void printList&lpar;Node head&rpar; &lbrace; &NewLine;&Tab;&Tab;while &lpar;head &excl;&equals; null&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;System&period;out&period;print&lpar;"-&gt&semi;"&plus;head&period;getData&lpar;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab;&Tab;head &equals; head&period;getNext&lpar;&rpar;&semi; &NewLine;&Tab; &rcub; &NewLine;&Tab;&Tab;System&period;out&period;println&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3>Approach 2&colon; Without Extra Space<&sol;h3>&NewLine;<p><strong>Step 1&colon;<&sol;strong> Get the middle of the linked list&period;<br &sol;>&NewLine;<strong>Step 2&colon;<&sol;strong> Reverse the second half of the linked list&period;<br &sol;>&NewLine;<strong>Step 3&colon;<&sol;strong> Check if the first half and second half are identical&period;<br &sol;>&NewLine;<strong>Step 4&colon;<&sol;strong> If all nodes matched&comma; then return true&comma; else false&period;<&sol;p>&NewLine;<p>Let&&num;8217&semi;s see the code<&sol;p>&NewLine;<pre class&equals;"lang&colon;java decode&colon;true ">&sol;&ast;&ast; &NewLine; &ast; This program will check palindrome linked list without using extra space &NewLine; &ast;&sol; &NewLine;package com&period;dineshonjava&period;algo&semi; &NewLine; &NewLine;import java&period;util&period;Optional&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class LinkedListTest &lbrace; &NewLine; &NewLine;&Tab;&sol;&ast;&ast; &NewLine;&Tab; &ast; &commat;param args &NewLine;&Tab; &ast;&sol; &NewLine;&Tab;public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine;&Tab;&Tab; &NewLine;&Tab;int arr&lbrack;&rsqb; &equals; &lbrace;1&comma; 2&comma; 3&comma; 2&comma; 1&rcub;&semi; &NewLine; Node head &equals; push&lpar;arr&rpar;&semi; &NewLine; &NewLine; &NewLine; if &lpar;isPalindromicLinkedListWithoutExtraSpace&lpar;head&rpar;&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Is Palindrome"&rpar;&semi; &NewLine; &rcub; else &lbrace; &NewLine; System&period;out&period;println&lpar;"Not Palindrome"&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;public static Node push&lpar;int arr&lbrack;&rsqb;&rpar; &lbrace; &NewLine;&Tab;&Tab; Node head &equals; new Node&lpar;String&period;valueOf&lpar;arr&lbrack;0&rsqb;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab; Node current &equals; head&semi; &NewLine;&Tab;&Tab; for &lpar;int i &equals; 1&semi; i&lt&semi; arr&period;length &semi; i&plus;&plus;&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab; Node newNode &equals; new Node&lpar;String&period;valueOf&lpar;arr&lbrack;i&rsqb;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab;&Tab; current&period;next &equals; newNode&semi; &NewLine;&Tab;&Tab;&Tab; current &equals; newNode&semi; &NewLine;&Tab;&Tab; &rcub; &NewLine;&Tab;&Tab; &Tab; &NewLine;&Tab;&Tab; return head&semi; &NewLine; &rcub; &NewLine;&Tab; &NewLine;&Tab;public static boolean isPalindromicLinkedListWithoutExtraSpace&lpar;Node head&rpar; &lbrace; &NewLine;&Tab;&Tab; &NewLine;&Tab;&Tab;if&lpar;head &equals;&equals; null &vert;&vert; head&period;next &equals;&equals; null&rpar; &NewLine;&Tab;&Tab;&Tab;return true&semi; &NewLine;&Tab;&Tab; &NewLine;&Tab;&Tab;Node slowPointer &equals; head&semi; &NewLine;&Tab; Node fastPointer &equals; head&semi; &NewLine;&Tab; Node first&lowbar;half &equals; head&semi; &NewLine;&Tab; Node second&lowbar;half &equals; null&semi; &NewLine;&Tab; while &lpar;fastPointer &excl;&equals; null &amp&semi;&amp&semi; fastPointer&period;next &excl;&equals; null&rpar; &lbrace; &NewLine;&Tab; fastPointer &equals; fastPointer&period;next&period;next&semi; &NewLine;&Tab; slowPointer &equals; slowPointer&period;next&semi; &NewLine;&Tab; &rcub; &NewLine;&Tab; &NewLine;&Tab; &sol;&sol;In case of ODD number &NewLine;&Tab; if&lpar;fastPointer &excl;&equals; null&rpar; &lbrace; &NewLine;&Tab; &Tab;slowPointer &equals; slowPointer&period;next&semi; &NewLine;&Tab; &rcub; &NewLine;&Tab; second&lowbar;half &equals; reverseUsingIteration&lpar;slowPointer&rpar;&semi; &NewLine;&Tab; &NewLine;&Tab; while &lpar;first&lowbar;half &excl;&equals; null &amp&semi;&amp&semi; second&lowbar;half &excl;&equals; null&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;if&lpar;Integer&period;valueOf&lpar;first&lowbar;half&period;data&rpar; &excl;&equals; Integer&period;valueOf&lpar;second&lowbar;half&period;data&rpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;&Tab;return false&semi; &NewLine;&Tab;&Tab;&Tab;&rcub; &NewLine;&Tab;&Tab;&Tab;second&lowbar;half &equals; second&lowbar;half&period;next&semi; &NewLine;&Tab;&Tab;&Tab;first&lowbar;half &equals; first&lowbar;half&period;next&semi; &NewLine;&Tab;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;&Tab;return true&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;public static void printList&lpar;Node head&rpar; &lbrace; &NewLine;&Tab;&Tab;while &lpar;head &excl;&equals; null&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;System&period;out&period;print&lpar;"-&gt&semi;"&plus;head&period;getData&lpar;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab;&Tab;head &equals; head&period;getNext&lpar;&rpar;&semi; &NewLine;&Tab; &rcub; &NewLine;&Tab;&Tab;System&period;out&period;println&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>Run above program&comma; you will get the following output&colon;<&sol;p>&NewLine;<pre class&equals;"">Is Palindrome &NewLine;<&sol;pre>&NewLine;<p>In this example&comma; we have checked palindrome singly linked list without using extra space&period;<&sol;p>&NewLine;<p>Hope&comma; you have understood this solution for the above find the middle node from a linked list&period; Please share other solutions if you have&period; &colon;&rpar;&period;<&sol;p>&NewLine;<p>Happy learning with us&excl;&excl;&excl;&period;<&sol;p>&NewLine;<div align&equals;"center"><iframe style&equals;"width&colon; 120px&semi; height&colon; 240px&semi;" src&equals;"&sol;&sol;ws-in&period;amazon-adsystem&period;com&sol;widgets&sol;q&quest;ServiceVersion&equals;20070822&amp&semi;OneJS&equals;1&amp&semi;Operation&equals;GetAdHtml&amp&semi;MarketPlace&equals;IN&amp&semi;source&equals;ac&amp&semi;ref&equals;tf&lowbar;til&amp&semi;ad&lowbar;type&equals;product&lowbar;link&amp&semi;tracking&lowbar;id&equals;dineshonjav06-21&amp&semi;marketplace&equals;amazon&amp&semi;region&equals;IN&amp&semi;placement&equals;1787127567&amp&semi;asins&equals;1787127567&amp&semi;linkId&equals;a1e64f621b5e6128d8cb10e876eb1c48&amp&semi;show&lowbar;border&equals;true&amp&semi;link&lowbar;opens&lowbar;in&lowbar;new&lowbar;window&equals;true&amp&semi;price&lowbar;color&equals;333333&amp&semi;title&lowbar;color&equals;0066c0&amp&semi;bg&lowbar;color&equals;ffffff" frameborder&equals;"0" marginwidth&equals;"0" marginheight&equals;"0" scrolling&equals;"no" align&equals;"center"><span data-mce-type&equals;"bookmark" style&equals;"display&colon; inline-block&semi; width&colon; 0px&semi; overflow&colon; hidden&semi; line-height&colon; 0&semi;" class&equals;"mce&lowbar;SELRES&lowbar;start"><&sol;span><br &sol;>&NewLine;<&sol;iframe><&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;print-nodes-at-k-distance-from-the-root&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;remove-duplicates-from-the-unsorted-singly-linked-list&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '4261'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago