Skip to content

Instantly share code, notes, and snippets.

@Shivamy2
Last active February 27, 2021 08:56
Show Gist options
  • Save Shivamy2/b89fddbaf64afe48d9bb9c7c5257e3de to your computer and use it in GitHub Desktop.
Save Shivamy2/b89fddbaf64afe48d9bb9c7c5257e3de to your computer and use it in GitHub Desktop.
import java.util.LinkedList;
import java.util.List;
class Node {
int data;
Node next;
Node prev;
}
class MyDoubleLinkedList {
Node head;
public void moveMiddleToTop() {
Node slowPointer = head;
Node fastPointer = slowPointer;
while(fastPointer.next != null || fastPointer.next.next != null) {
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
}
Node temp = head;
Node beforeSlowPointer = slowPointer.prev;
beforeSlowPointer.next = slowPointer.next;
slowPointer.next = head.next;
head = slowPointer;
}
}
import java.util.LinkedList;
import java.util.List;
class Main {
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<>(List.of(1,5,2,3,7,87,345,23,342,5,3,34,5,9));
System.out.println(occuranceCount(linkedList, 3));
}
public static int occuranceCount(LinkedList<Integer> linkedList ,int data) {
int count = 0;
for (int i = 0; i < linkedList.size(); i++) {
if(linkedList.get(i) == data) count++;
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment