-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99eda4a
commit 5cd6e27
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Created by ashankar1 on 10/16/15. | ||
*/ | ||
|
||
/** | ||
* A Singly Linked List node | ||
*/ | ||
class ListNode{ | ||
int value; | ||
ListNode next; | ||
|
||
ListNode(int val){ | ||
this.value = val; | ||
this.next = null; | ||
} | ||
} | ||
public class LinkedList { | ||
public static void main(String[] args){ | ||
ListNode head = createList(10); | ||
printList(head); | ||
System.out.println(); | ||
findMid(head); | ||
ListNode reversedList = reverseList(head); | ||
printList(reversedList); | ||
System.out.println(); | ||
} | ||
|
||
/** | ||
* Creates a list of n elements | ||
* @param n number of elements in the list | ||
* @return head of the list | ||
*/ | ||
private static ListNode createList(int n) { | ||
ListNode head = new ListNode(1); | ||
ListNode last = head; | ||
for(int i=2;i<n+1;i++){ | ||
last.next = new ListNode(i); | ||
last = last.next; | ||
} | ||
return head; | ||
} | ||
|
||
/** | ||
* Prints the linked list | ||
* @param head | ||
*/ | ||
private static void printList(ListNode head){ | ||
ListNode current = head; | ||
while(current!=null) { | ||
System.out.print(current.value+" "); | ||
current = current.next; | ||
} | ||
} | ||
|
||
/** | ||
* Reverse the linkedlist | ||
* @param head linked list to be reversed | ||
* @return head of the reversed linked list | ||
*/ | ||
private static ListNode reverseList(ListNode head){ | ||
ListNode prev = head; | ||
ListNode current = head.next; | ||
ListNode temp = null; | ||
prev.next = null; | ||
while(current != null){ | ||
temp = current.next; | ||
current.next = prev; | ||
prev = current; | ||
current = temp; | ||
} | ||
return prev; | ||
} | ||
|
||
/** | ||
* Finds the middle element of the given linked list | ||
* @param head linked list | ||
* @return the middle node of the linked list | ||
*/ | ||
private static ListNode findMid(ListNode head){ | ||
ListNode slowPtr = head; | ||
ListNode fastPtr = head; | ||
|
||
while(fastPtr.next != null && fastPtr.next.next != null){ | ||
slowPtr = slowPtr.next; | ||
fastPtr = fastPtr.next.next; | ||
} | ||
System.out.println("Mid point "+slowPtr.value); | ||
return slowPtr; | ||
} | ||
} |