code_junkie

Reversing a Linked List in Java, recursively

Nov 12th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. public class ListNode{
  2. public String data;
  3. public ListNode next;
  4. }
  5.  
  6. public AddressList reverse(){
  7. return new AddressList(this.reverse(this.head));
  8. }
  9.  
  10. private AddressList reverse(ListNode current, AddressList reversedList){
  11. if(current == null) return reversedList;
  12. reversedList.addToFront(current.getData());
  13. return this.reverse(current.getNext(), reversedList);
  14. }
  15.  
  16. public ListNode Reverse(ListNode list)
  17. {
  18. if (list == null) return null; // first question
  19.  
  20. if (list.next == null) return list; // second question
  21.  
  22. // third question - in Lisp this is easy, but we don't have cons
  23. // so we grab the second element (which will be the last after we reverse it)
  24.  
  25. ListNode secondElem = list.next;
  26.  
  27. // bug fix - need to unlink list from the rest or you will get a cycle
  28. list.next = null;
  29.  
  30. // then we reverse everything from the second element on
  31. ListNode reverseRest = Reverse(secondElem);
  32.  
  33. // then we join the two lists
  34. secondElem.Next = list;
  35.  
  36. return reverseRest;
  37. }
  38.  
  39. //Takes as parameters a node in a linked list, and p, the previous node in that list
  40. //returns the head of the new list
  41. Node reverse(Node n,Node p){
  42. if(n==null) return null;
  43. if(n.next==null){ //if this is the end of the list, then this is the new head
  44. n.next=p;
  45. return n;
  46. }
  47. Node r=reverse(n.next,n); //call reverse for the next node,
  48. //using yourself as the previous node
  49. n.next=p; //Set your next node to be the previous node
  50. return r; //Return the head of the new list
  51. }
  52.  
  53. //Takes as parameters a node in a linked list, and p, the previous node in that list
  54. //returns the head of the new list
  55. Node reverse(Node n,Node p){
  56. if(n==null) return null;
  57. if(n.next==null){ //if this is the end of the list, then this is the new head
  58. n.next=p;
  59. return n;
  60. }
  61. Node r=reverse(n.next,n); //call reverse for the next node,
  62. //using yourself as the previous node
  63. n.next=p; //Set your next node to be the previous node
  64. return r; //Return the head of the new list
  65. }
  66.  
  67. //Example:
  68. // reverse0(1->2->3, null) =>
  69. // reverse0(2->3, 1) =>
  70. // reverse0(3, 2->1) => reverse0(null, 3->2->1)
  71. // once the first argument is null, return the second arg
  72. // which is nothing but the reveresed list.
  73.  
  74. Link reverse0(Link f, Link n) {
  75. if (f != null) {
  76. Link t = new Link(f.data1, f.data2);
  77. t.nextLink = n;
  78. f = f.nextLink; // assuming first had n elements before,
  79. // now it has (n-1) elements
  80. reverse0(f, t);
  81. }
  82. return n;
  83. }
  84.  
  85. public Node reverse(Node previous, Node current) {
  86. if(previous == null)
  87. return null;
  88. if(previous.equals(head))
  89. previous.setNext(null);
  90. if(current == null) { // end of list
  91. head = previous;
  92. return head;
  93. } else {
  94. current.setNext(previous);
  95. reverse(current, current.getNext());
  96. }
  97. return null; //should never reach here.
  98. }
  99.  
  100. Node newHead = reverse(head, head.getNext());
  101.  
  102. |
  103. Head
  104.  
  105.  
  106.  
  107. public ListNode reverse(ListNode toBeNextNode, ListNode currentNode){
  108.  
  109. ListNode currentHead = currentNode; // keep track of the head
  110. if((currentNode==null ||currentNode.next==null )&& toBeNextNode ==null)return currentHead; // ignore for size 0 & 1
  111. if(currentNode.next!=null)currentHead = reverse(currentNode, currentNode.next); // travarse till end recursively
  112. currentNode.next = toBeNextNode; // reverse link
  113. return currentHead;
  114.  
  115.  
  116. }
  117.  
  118. Node reverse(Node head) {
  119. // if head is null or only one node, it's reverse of itself.
  120. if ( (head==null) || (head.next == null) ) return head;
  121.  
  122. // reverse the sub-list leaving the head node.
  123. Node reverse = reverse(head.next);
  124.  
  125. // head.next still points to the last element of reversed sub-list.
  126. // so move the head to end.
  127. head.next.next = head;
  128.  
  129. // point last node to nil, (get rid of cycles)
  130. head.next = null;
  131. return reverse;
  132. }
  133.  
  134. class Node<T>
  135. {
  136. Node<T> next;
  137. public T data;
  138. }
  139.  
  140. class LinkedList<T>
  141. {
  142. Node<T> head = null;
  143.  
  144. public void Reverse()
  145. {
  146. if (head != null)
  147. head = RecursiveReverse(null, head);
  148. }
  149.  
  150. private Node<T> RecursiveReverse(Node<T> prev, Node<T> curr)
  151. {
  152. Node<T> next = curr.next;
  153. curr.next = prev;
  154. return (next == null) ? curr : RecursiveReverse(curr, next);
  155. }
  156. }
  157.  
  158. class Node<T>
  159. {
  160. Node<T> next;
  161. public T data;
  162. }
  163.  
  164. class LinkedList<T>
  165. {
  166. Node<T> head = null;
  167.  
  168. public void Reverse()
  169. {
  170. if (head != null)
  171. head = RecursiveReverse(null, head);
  172. }
  173.  
  174. private Node<T> RecursiveReverse(Node<T> prev, Node<T> curr)
  175. {
  176. Node<T> next = curr.next;
  177. curr.next = prev;
  178. return (next == null) ? curr : RecursiveReverse(curr, next);
  179. }
  180. }
  181.  
  182. public Node reverseListRecursive(Node curr)
  183. {
  184. if(curr == null){//Base case
  185. return head;
  186. }
  187. else{
  188. (reverseListRecursive(curr.next)).next = (curr);
  189. }
  190. return curr;
  191. }
  192.  
  193. public class Singlelinkedlist {
  194. public static void main(String[] args) {
  195. Elem list = new Elem();
  196. Reverse(list); //list is populate some where or some how
  197. }
  198.  
  199. //this is the part you should be concerned with the function/Method has only 3 lines
  200.  
  201. public static void Reverse(Elem e){
  202. if (e!=null)
  203. if(e.next !=null )
  204. Reverse(e.next);
  205. //System.out.println(e.data);
  206. }
  207. }
  208.  
  209. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  210.  
  211. class Elem {
  212. public Elem next; // Link to next element in the list.
  213. public String data; // Reference to the data.
  214. }
Add Comment
Please, Sign In to add comment