-
Notifications
You must be signed in to change notification settings - Fork 0
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
eeec2c3
commit 481a4f4
Showing
4 changed files
with
113 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,101 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
class Node | ||
{ | ||
Node next; | ||
int data; | ||
|
||
Node(int d) | ||
{ | ||
data = d; | ||
next = null; | ||
} | ||
} | ||
class Delete_Kth_Node | ||
{ | ||
Node head; | ||
Node tail; | ||
|
||
void addToTheLast(Node node){ | ||
if(head == null){ | ||
head = node; | ||
tail = node; | ||
} | ||
else | ||
{ | ||
tail.next = node; | ||
tail = tail.next ; | ||
} | ||
} | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t=sc.nextInt(); | ||
while(t>0) | ||
{ | ||
int n = sc.nextInt(); | ||
Delete_Kth_Node list = new Delete_Kth_Node(); | ||
//int n=Integer.parseInt(br.readLine()); | ||
int a1=sc.nextInt(); | ||
Node head= new Node(a1); | ||
list.addToTheLast(head); | ||
for (int i = 1; i < n; i++) | ||
{ | ||
int a = sc.nextInt(); | ||
list.addToTheLast(new Node(a)); | ||
} | ||
int k = sc.nextInt(); | ||
//System.out.println(list.head.data+" "+ k); | ||
list.head = new Solution().delete(list.head,k); | ||
Node temp = list.head; | ||
while(temp!=null) | ||
{ | ||
System.out.print(temp.data+ " "); | ||
temp = temp.next; | ||
} | ||
System.out.println(); | ||
t--; | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
/* Link list Node | ||
class Node | ||
{ | ||
Node next; | ||
int data; | ||
Node(int d) | ||
{ | ||
data = d; | ||
next = null; | ||
} | ||
}*/ | ||
class Solution | ||
{ | ||
/*You are required to complete this method*/ | ||
Node delete(Node head, int k) | ||
{ | ||
// Your code here | ||
Node prev = new Node(-1); | ||
Node temp = prev; | ||
prev.next = head; | ||
int cnt = 1; | ||
while(temp.next!=null) | ||
{ | ||
if(cnt==k) | ||
{ | ||
temp.next = temp.next.next; | ||
cnt = 0; | ||
} | ||
else | ||
{ | ||
temp = temp.next; | ||
} | ||
cnt++; | ||
} | ||
|
||
return prev.next; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
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,8 @@ | ||
class Solution | ||
{ | ||
public int minOperations(int[] nums, int k) | ||
{ | ||
int xors = Arrays.stream(nums).reduce((a, b) -> a ^ b).getAsInt(); | ||
return Integer.bitCount(k ^ xors); | ||
} | ||
} |