Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ListNode{
- public String data;
- public ListNode next;
- }
- public AddressList reverse(){
- return new AddressList(this.reverse(this.head));
- }
- private AddressList reverse(ListNode current, AddressList reversedList){
- if(current == null) return reversedList;
- reversedList.addToFront(current.getData());
- return this.reverse(current.getNext(), reversedList);
- }
- public ListNode Reverse(ListNode list)
- {
- if (list == null) return null; // first question
- if (list.next == null) return list; // second question
- // third question - in Lisp this is easy, but we don't have cons
- // so we grab the second element (which will be the last after we reverse it)
- ListNode secondElem = list.next;
- // bug fix - need to unlink list from the rest or you will get a cycle
- list.next = null;
- // then we reverse everything from the second element on
- ListNode reverseRest = Reverse(secondElem);
- // then we join the two lists
- secondElem.Next = list;
- return reverseRest;
- }
- //Takes as parameters a node in a linked list, and p, the previous node in that list
- //returns the head of the new list
- Node reverse(Node n,Node p){
- if(n==null) return null;
- if(n.next==null){ //if this is the end of the list, then this is the new head
- n.next=p;
- return n;
- }
- Node r=reverse(n.next,n); //call reverse for the next node,
- //using yourself as the previous node
- n.next=p; //Set your next node to be the previous node
- return r; //Return the head of the new list
- }
- //Takes as parameters a node in a linked list, and p, the previous node in that list
- //returns the head of the new list
- Node reverse(Node n,Node p){
- if(n==null) return null;
- if(n.next==null){ //if this is the end of the list, then this is the new head
- n.next=p;
- return n;
- }
- Node r=reverse(n.next,n); //call reverse for the next node,
- //using yourself as the previous node
- n.next=p; //Set your next node to be the previous node
- return r; //Return the head of the new list
- }
- //Example:
- // reverse0(1->2->3, null) =>
- // reverse0(2->3, 1) =>
- // reverse0(3, 2->1) => reverse0(null, 3->2->1)
- // once the first argument is null, return the second arg
- // which is nothing but the reveresed list.
- Link reverse0(Link f, Link n) {
- if (f != null) {
- Link t = new Link(f.data1, f.data2);
- t.nextLink = n;
- f = f.nextLink; // assuming first had n elements before,
- // now it has (n-1) elements
- reverse0(f, t);
- }
- return n;
- }
- public Node reverse(Node previous, Node current) {
- if(previous == null)
- return null;
- if(previous.equals(head))
- previous.setNext(null);
- if(current == null) { // end of list
- head = previous;
- return head;
- } else {
- current.setNext(previous);
- reverse(current, current.getNext());
- }
- return null; //should never reach here.
- }
- Node newHead = reverse(head, head.getNext());
- |
- Head
- public ListNode reverse(ListNode toBeNextNode, ListNode currentNode){
- ListNode currentHead = currentNode; // keep track of the head
- if((currentNode==null ||currentNode.next==null )&& toBeNextNode ==null)return currentHead; // ignore for size 0 & 1
- if(currentNode.next!=null)currentHead = reverse(currentNode, currentNode.next); // travarse till end recursively
- currentNode.next = toBeNextNode; // reverse link
- return currentHead;
- }
- Node reverse(Node head) {
- // if head is null or only one node, it's reverse of itself.
- if ( (head==null) || (head.next == null) ) return head;
- // reverse the sub-list leaving the head node.
- Node reverse = reverse(head.next);
- // head.next still points to the last element of reversed sub-list.
- // so move the head to end.
- head.next.next = head;
- // point last node to nil, (get rid of cycles)
- head.next = null;
- return reverse;
- }
- class Node<T>
- {
- Node<T> next;
- public T data;
- }
- class LinkedList<T>
- {
- Node<T> head = null;
- public void Reverse()
- {
- if (head != null)
- head = RecursiveReverse(null, head);
- }
- private Node<T> RecursiveReverse(Node<T> prev, Node<T> curr)
- {
- Node<T> next = curr.next;
- curr.next = prev;
- return (next == null) ? curr : RecursiveReverse(curr, next);
- }
- }
- class Node<T>
- {
- Node<T> next;
- public T data;
- }
- class LinkedList<T>
- {
- Node<T> head = null;
- public void Reverse()
- {
- if (head != null)
- head = RecursiveReverse(null, head);
- }
- private Node<T> RecursiveReverse(Node<T> prev, Node<T> curr)
- {
- Node<T> next = curr.next;
- curr.next = prev;
- return (next == null) ? curr : RecursiveReverse(curr, next);
- }
- }
- public Node reverseListRecursive(Node curr)
- {
- if(curr == null){//Base case
- return head;
- }
- else{
- (reverseListRecursive(curr.next)).next = (curr);
- }
- return curr;
- }
- public class Singlelinkedlist {
- public static void main(String[] args) {
- Elem list = new Elem();
- Reverse(list); //list is populate some where or some how
- }
- //this is the part you should be concerned with the function/Method has only 3 lines
- public static void Reverse(Elem e){
- if (e!=null)
- if(e.next !=null )
- Reverse(e.next);
- //System.out.println(e.data);
- }
- }
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- class Elem {
- public Elem next; // Link to next element in the list.
- public String data; // Reference to the data.
- }
Add Comment
Please, Sign In to add comment