Skip to content

Commit

Permalink
Singly Linked List operations
Browse files Browse the repository at this point in the history
  • Loading branch information
anjanashankar9 committed Oct 16, 2015
1 parent 99eda4a commit 5cd6e27
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions LinkedList/src/LinkedList.java
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;
}
}

0 comments on commit 5cd6e27

Please sign in to comment.