Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reverse a linked list and return pointer to the head
- The input list will have at least one element
- Node is defined as
- class Node {
- int data;
- Node next;
- }
- */
- Node Reverse(Node head) {
- if(head == null || head.next == null)
- return head;
- Node p = null;
- Node tail= null;
- while(head != null){
- p = head.next;
- head.next = tail;
- tail = head;
- head = p;
- }
- return tail;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement