My Way of Computing
30 Ekim 2019 Çarşamba
LinkedList Implementations
Reverse LinkedList with using Recursion
package ornek01; public class LinkedList01 { static Node head; static class Node{ int data; Node next; Node(int d){ this.data = d; } } static Node reverseList(Node currNode, Node prev) { if(currNode.next == null) { currNode.next = prev; head = currNode; return head; } Node n_node = currNode.next; currNode.next = prev; reverseList(n_node, currNode); return head; } static void printList(Node node) { while(node !=null) { System.out.println(node.data + " "); node = node.next; } } public static void main(String[] args) { LinkedList01 list = new LinkedList01(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); list.head.next.next.next = new Node(4); list.head.next.next.next.next = new Node(5); list.head.next.next.next.next.next = new Node(6); list.head.next.next.next.next.next.next = new Node(7); list.head.next.next.next.next.next.next.next = new Node(8); System.out.println("Original Linked list "); list.printList(head); System.out.println("\nAfter reverse"); reverseList(head, null); printList(head); } }
Daha Yeni Kayıtlar
Önceki Kayıtlar
Ana Sayfa
Kaydol:
Kayıtlar (Atom)