diff --git a/Java/DataStructures/LinkedList.java b/Java/DataStructures/LinkedList.java index 8b5145c6..eabd1270 100644 --- a/Java/DataStructures/LinkedList.java +++ b/Java/DataStructures/LinkedList.java @@ -152,7 +152,8 @@ public int indexOfLastAppearance(int value) { } return position; } - + + /* Other Methods */ @@ -169,7 +170,34 @@ public boolean contains(int value) { } return false; } + /*Mid of a linked list*/ + public Node midNode(Node head) { + if (head == null || head.next == null) + return head; + + Node slow = head, fast = head; + while (fast.next != null && fast.next.next != null) { + slow = slow.next; + fast = fast.next.next; + } + + return slow; + } + + public Node midNode2(Node head) { + if (head == null || head.next == null) + return head; + + Node slow = head, fast = head; + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + + return slow; + } + public int size() { return size; }