{"id":5744,"date":"2026-02-09T10:14:46","date_gmt":"2026-02-09T10:14:46","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5744"},"modified":"2026-02-09T10:29:25","modified_gmt":"2026-02-09T10:29:25","slug":"simple-linked-list-operations","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/simple-linked-list-operations\/","title":{"rendered":"Simple Linked List Operations"},"content":{"rendered":"<ol>\n<li>Linked List is made of combination of Node<\/li>\n<li>The smallest unit of linked list is Node<\/li>\n<li>Node contains two things the content of the node And reference to next node<\/li>\n<li>If a linked list has only one element then it would have only one node<\/li>\n<\/ol>\n<p><strong>Node.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass Node{\r\n\tString content;\r\n\tNode next;\r\n\r\n        public Node(String content){\r\n \t this.content = content;\r\n\t}\t\r\n}\r\n<\/pre>\n<ol>\n<li>To traverse linked list we should always start at the head <\/li>\n<li>After printing the content we should move to next node as mentioned in next variable in current node<\/li>\n<\/ol>\n<p><strong>Traversing a LL<\/strong><br \/>\nLL node next always points to reference of next node if any or else would be null if only one element is present. While Traversing we always start with head position<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Main{\r\n\tpublic static void main(String args&#x5B;]){\r\n\t\tNode node1 = new Node(&quot;Anbu&quot;);\r\n\t\t\t\t\r\n\t\tNode node2 = new Node(&quot;Arul&quot;);\r\n\t\tnode1.next = node2;\r\n\r\n\t\tNode node3 = new Node(&quot;Arasu&quot;);\r\n\t\tnode2.next = node3;\r\n\r\n\t\tprintLinkedList(node1);\r\n\t}\r\n\r\n\tpublic static void printLinkedList(Node startingNode){\r\n\t\twhile(startingNode.next !=null){\r\n\t\t\tSystem.out.println(startingNode.content);\r\n\t\t\tstartingNode = startingNode.next;\r\n\t\t}\r\n\r\n\t\tif(startingNode.next == null){\r\n\t\t\tSystem.out.println(startingNode.content);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>The above code can be refactored as below using recursion calling same function again and again<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Main{\r\n\tpublic static void main(String args&#x5B;]){\r\n\t\tNode node1 = new Node(&quot;Anbu&quot;);\r\n\t\t\t\t\r\n\t\tNode node2 = new Node(&quot;Arul&quot;);\r\n\t\tnode1.next = node2;\r\n\r\n\t\tNode node3 = new Node(&quot;Arasu&quot;);\r\n\t\tnode2.next = node3;\r\n\r\n\t\tprintLinkedList(node1.head);\r\n\t}\r\n        \r\n        public static void printLinkedList(Node startingNode){\r\n\t\t\/\/Note: We are traversing to next null node before exit without this last element wont be printed\r\n\t\tif(startingNode == null){ \t\t\t\r\n\t\t  return;\r\n\t\t}\r\n\r\n\t\tSystem.out.println(startingNode.content);\r\n\t\tprintLinkedList(startingNode.next);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nAnbu\r\nArul\r\nArasu\r\n<\/pre>\n<p>Now let&#8217;s move the linked list methods to a separate class called SimpleLinkedList.java<\/p>\n<p><strong>SimpleLinkedList.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SimpleLinkedList {\r\n    Node head;\r\n\r\n    public static void traverseLinkedList(Node startingNode){\r\n        \/\/Note: We are traversing to next null node before exit without this last element wont be printed\r\n        if(startingNode == null){\r\n            return;\r\n        }\r\n\r\n        System.out.println(startingNode.content);\r\n        printLinkedList(startingNode.next);\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Linked List is made of combination of Node The smallest unit of linked list is Node Node contains two things the content of the node And reference to next node If a linked list has only one element then it would have only one node Node.java class Node{ String content; Node next; public Node(String content){&hellip; <a href=\"https:\/\/codethataint.com\/blog\/simple-linked-list-operations\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[229],"tags":[],"class_list":["post-5744","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5744"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5744\/revisions"}],"predecessor-version":[{"id":5746,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5744\/revisions\/5746"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}