GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Programs/ReverseList.java at main · arshcoder13/Java Expand file tree/ Copy pathReverseList.javaMore file actions 74 lines (74 loc) · 2.35 KB / Copy pathReverseList.javaFile metadata and controls74 lines (74 loc) · 2.35 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 // In this code a linkedlist is taken as input and the reversed form is displayed import java.util.*; public class ReverseList { public static void main(String args[]) { try (Scanner sc = new Scanner(System.in)) { ListNode fresh,start=null,prev=null,ptr=null; int c; do { fresh=new ListNode(); System.out.println("Enter the data"); fresh.data=sc.nextInt(); if(start==null) { start=fresh; // stores the starting address } else { prev.next=fresh; } prev=fresh; System.out.println("Press 1 to for next node initialization else press any number other than 1 to exit"); c=sc.nextInt(); // for next node initialization option is asked from the user }while(c==1); Solution nm=new Solution(); System.out.println("The original LinkedList is "); for(ptr=start;ptr!=null;ptr=ptr.next) { System.out.print(ptr.data+" "); } System.out.println(); // displayes the original linkedlist System.out.println("The reversed LinkedList is "); nm.reverseList(start); for(ptr=start;ptr!=null;ptr=ptr.next) { System.out.print(ptr.data+" "); } System.out.println(); // displays the reserved linkedlist } } } class Solution { public ListNode reverseList(ListNode start) { if(start==null) return null; // if there is no elements in the linkedlist then it returns null else { ListNode ptr,ptr1,ptr2; for(ptr=null,ptr1=start,ptr2=start.next,ptr1.next=null;ptr2!=null;ptr2=ptr2.next,ptr1.next=ptr) { ptr=ptr1; ptr1=ptr2; } start=ptr1; return start; // returns the start position of the linkedlist after reversing the list } } } // Solution class contains reverseList method which takes the start address as input and reverses the whole linkedlist class ListNode { int data; ListNode next; } // ListNode class is created for linklist creation |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |