Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create singlyLLrecursive.c #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Linked_list/singlyLLrecursive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class SinglyLinkedList { private Node head; // Head is the first node in linked list public void append(T data){ if(head == null){ head = new Node(data); return; } tail().next = new Node(data); } private Node tail() { Node tail = head; // Find last element of linked list known as tail while(tail.next != null){ tail = tail.next; } return tail; } @Override public String toString(){ StringBuilder sb = new StringBuilder(); Node current = head; while(current != null){ sb.append(current).append("-->"); current = current.next; } if(sb.length() >=3){ sb.delete(sb.length() - 3, sb.length()); // to remove --> from last node } return sb.toString(); } /** * Reverse linked list using 3 pointers approach in O(n) time * It basically creates a new list by reversing direction, and * subsequently insert the element at the start of the list. */ public void reverseIteratively() { Node current = head; Node previous = null; Node forward = null; // traversing linked list until there is no more element while(current.next != null){ // Saving reference of next node, since we are changing current node forward = current.next; // Inserting node at start of new list current.next = previous; previous = current; // Advancing to next node current = forward; } head = current; head.next = previous; } /* * Reverse a singly linked list using recursion. In recursion Stack is * used to store data. * 1. Traverse linked list till we find the tail, * that would be new head for reversed linked list. */ private Node reverseRecursively(Node node){ Node newHead; //base case - tail of original linked list if((node.next == null)){ return node; } newHead = reverseRecursively(node.next); //reverse the link e.g. C->D->null will be null node.next.next = node; node.next = null; return newHead; } public void reverseRecursively(){ head = reverseRecursively(head); } private static class Node { private Node next; private T data; public Node(T data) { this.data = data; } @Override public String toString() { return data.toString(); } } }