Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ListNode{
- int value;
- ListNode next;
- ListNode(int val){
- value = val;
- next = null;
- }
- }
- public class Solution{
- static ListNode makeList(int size){
- ListNode head = new ListNode(1);
- ListNode prev = head;
- for(int i=2;i<=size;++i){
- ListNode current = new ListNode(i);
- prev.next = current;
- prev = current;
- }
- return head;
- }
- static ListNode reverseList(ListNode head){
- ListNode prev = null,current=head,next_node = null;
- while(current != null){
- next_node = current.next;
- current.next = prev;
- prev = current;
- current = next_node;
- }
- return prev;
- }
- static void printList(ListNode head){
- System.out.println();
- while(head != null){
- System.out.print(head.value + "-->");
- head = head.next;
- }
- System.out.print("NULL\n");
- }
- public static void main(String[] args){
- ListNode head = makeList(Integer.parseInt(args[0]));
- printList(head);
- head = reverseList(head);
- printList(head);
- }
- }
Add Comment
Please, Sign In to add comment