Skip to content

Commit

Permalink
Added codes for 5 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 5, 2024
1 parent 441f8a4 commit 6111296
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
118 changes: 118 additions & 0 deletions GeeksForGeeks/February/5-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//{ Driver Code Starts
import java.util.Scanner;

// Node Class
class Node {
int data;
Node next;

Node(int x) {
data = x;
next = null;
}
}



public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();

while (t-- > 0) {
int n = sc.nextInt();
Node start = null;
Node temp=null, r = null;

// Create linked list from the array arr[].
// Created linked list will be 1->2->11->56->12
if (n > 0) {
int arr = sc.nextInt();

temp = new Node(arr);
start = temp;
r = start;
}

for (int i = 0; i < n - 1; i++) {
int arr = sc.nextInt();
temp = new Node(arr);
r.next = temp;
r = r.next;
}

if (n > 0)
{
temp.next = start;
r = temp;
}


int x = sc.nextInt();
Solution ob = new Solution();
start = ob.sortedInsert(start, x);
printList(start);
r = start;
while (r != start.next) {
temp = start;
start = start.next;
temp = null;
}
temp = null;
}
}

/* Function to print Nodes in a given linked list */
static void printList(Node start) {
Node temp;

if (start != null) {
temp = start;
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != start);
System.out.println();
}
}
}

// } Driver Code Ends


//User function Template for Java
class Solution
{
public Node sortedInsert(Node head, int data)
{
// code here
Node current = head;
Node new_Node = new Node(data);

if (current == null)
{
new_Node.next = new_Node;
return new_Node;
}

else if (current.data >= new_Node.data)
{
while (current.next != head)
current = current.next;

current.next = new_Node;
new_Node.next = head;
return new_Node;
}

else
{
while (current.next != head && current.next.data < new_Node.data)
current = current.next;

new_Node.next = current.next;
current.next = new_Node;
return head;
}
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/5-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/February/5-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
16 changes: 16 additions & 0 deletions LeetCode/February/5-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution
{
public int firstUniqChar(String s)
{
int[] count = new int[26];

for (final char c : s.toCharArray())
++count[c - 'a'];

for (int i = 0; i < s.length(); ++i)
if (count[s.charAt(i) - 'a'] == 1)
return i;

return -1;
}
}

0 comments on commit 6111296

Please sign in to comment.