Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9eee724

Browse files
committed
update on some questios
New Linked List exercises on leetcode and book
1 parent ff3d3e2 commit 9eee724

File tree

9 files changed

+325
-0
lines changed

9 files changed

+325
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
3+
import org.w3c.dom.Node;
4+
5+
/*Check whether the given linked list is null-terminated or not
6+
* If there is a cycle find the start node of the loop
7+
*/
8+
/*SOlUTION: extended version of Floyd cycle finding algorithm
9+
* After finding the loop in the
10+
linked list, we initialize the slowPtr to the head of the linked list. From that point onwards both
11+
slowPtr and fastPtr move only one node at a time. The point at which they meet is the start of the loop.
12+
*/
13+
public class Problem12 {
14+
public static void main(String[] args) {
15+
LinkedList list= new LinkedList();
16+
list.insert(3, 6);
17+
for(int i=5; i> 0; i--){
18+
ListNode node= new ListNode(i);
19+
list.insertAtBeginning(node);
20+
}
21+
22+
23+
System.out.println(list.toString());
24+
25+
26+
}
27+
28+
public static ListNode findBeginOfLoop(ListNode head){
29+
ListNode fast=head;
30+
ListNode slow=head;
31+
boolean loopExists=false;
32+
while(fast != null && fast.getNext() != null){ //while linked list is not empty and head is not null
33+
fast= fast.getNext().getNext();// double jump, travels at 2x rather than once
34+
slow= slow.getNext();
35+
if(slow == fast){ //when both slow and fast node are equals it means a loop is reached asserting that its a circular linked list
36+
loopExists=true; //we assign to true and jump of the loop
37+
break;
38+
}
39+
}
40+
41+
if(loopExists){ //while loop is is
42+
slow=head; //get slow back to head
43+
while(slow != fast){ //while slow and fast are not equal now both travel one jump at time until both pointers are in the loop at the start loop position
44+
slow=slow.getNext();
45+
fast=fast.getNext();
46+
}
47+
48+
return fast; //return the pointer where the cycle starts
49+
}
50+
51+
return null;
52+
}
53+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
/*
3+
* Check whether the given linked list is NULL-terminated. If there is a cycle, find the length of the loop.
4+
*/
5+
/*
6+
* This solution is also an extension of the basic cycle detection problem. After finding the
7+
loop in the linked list, keep the slowPtr as it is. The fastPtr keeps on moving until it again comes
8+
back to slowPtr. While moving fastPtr, use a counter variable which increments at the rate of 1
9+
*/
10+
public class Problem15 {
11+
public static void main(String[] args) {
12+
13+
}
14+
15+
public static int findLengthOfTheLoop(ListNode head){
16+
ListNode slow= head;
17+
ListNode fast= head;
18+
int length=0;
19+
boolean isLoop=false;
20+
21+
while(slow != null && fast != null){
22+
slow= slow.getNext();
23+
fast=fast.getNext().getNext();
24+
if(slow== fast){
25+
isLoop=true;
26+
break;
27+
}
28+
}
29+
30+
if(isLoop){
31+
length=1;
32+
fast =slow.getNext();
33+
while(slow != fast){
34+
fast=fast.getNext();
35+
length++;
36+
}
37+
38+
}
39+
40+
return length;
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
/* Insert a node in a sorted linked list
3+
* Solution: Traverse the list and find a position for the element and insert it.
4+
*/
5+
public class Problem16 {
6+
public static void main(String[] args) {
7+
8+
}
9+
10+
public static ListNode inserInSortedList(ListNode head, ListNode newNode){
11+
ListNode current= head;
12+
13+
if(head == null){
14+
head= newNode;
15+
return newNode;
16+
}
17+
18+
while(current != null && current.getData() < newNode.getData()){
19+
ListNode temp= current.getNext();
20+
newNode.setNext(temp);
21+
current.setNext(newNode);
22+
}
23+
24+
return head;
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
/* Reverse a singly linked list.*/
3+
public class Problem17 {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
public static ListNode reverseListIterative(ListNode head){
9+
ListNode newHead= null;
10+
11+
while(head != null){
12+
ListNode next= head.getNext();
13+
head.setNext(newHead);
14+
newHead= head;
15+
head= next;
16+
}
17+
18+
return newHead;
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
3+
import java.util.List;
4+
5+
public class Problem2 {
6+
public static void main(String[] args) {
7+
8+
}
9+
10+
//solution from problem 5
11+
public static ListNode NthNodeFromEnd(ListNode head, int NthNode){
12+
ListNode pTemp=head; //temporary node
13+
ListNode pNthNode= null; //counts the number of nodes
14+
15+
for(int i=1; i<NthNode; i++){
16+
if(pTemp != null){
17+
pTemp= pTemp.getNext(); //if temporary pointer is not null get the next node until we find the end of the list
18+
}
19+
}
20+
21+
while(pTemp!=null){
22+
if(pNthNode==null){
23+
pNthNode= head;
24+
}else{
25+
pNthNode=pNthNode.getNext();
26+
}
27+
28+
pTemp= pTemp.getNext();
29+
}
30+
31+
if(pNthNode != null){
32+
return pNthNode;
33+
}
34+
35+
return null;
36+
}
37+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
3+
public class Problem24 {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
9+
int l1=0;
10+
int l2=0;
11+
int diff=0;
12+
ListNode head1= headA;
13+
ListNode head2= headB;
14+
15+
while(head1 != null){
16+
l1++;
17+
head1= head1.next;
18+
}
19+
20+
while(head2 != null){
21+
l2++;
22+
head2=head2.next;
23+
}
24+
25+
if(l1 < l2){
26+
head1=headB;
27+
head2=headA;
28+
diff= l2-l1;
29+
}else{
30+
head1=headA;
31+
head2= headB;
32+
diff= l1-l2;
33+
}
34+
35+
for(int i=0; i< diff; i++){
36+
head1= head1.next;
37+
while(head1 != null && head2 != null){
38+
if(head1==head2){
39+
return head1;
40+
}
41+
42+
head1= head1.next;
43+
head2=head2.next;
44+
}
45+
}
46+
47+
return null;
48+
}
49+
}

‎LeetCode/Blind75/Lt19.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Blind75;
2+
//Definition for singly-linked list.
3+
class ListNode {
4+
int val;
5+
ListNode next;
6+
ListNode() {}
7+
ListNode(int val) { this.val = val; }
8+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
}
10+
public class Lt19 {
11+
public static void main(String[] args) {
12+
13+
}
14+
15+
//get previous to node to the node be deleted, point to node next to the node to be deleted
16+
//and point node to be deleted to null
17+
// 1 -> 2 -> 3 -> 5 -> null
18+
// 4 -> null
19+
//return deleted node
20+
/*We can use the turtle and rabbit algoritm from cycle list, so the faster pointer reaches faster to node to be deleted
21+
* and slow pointer will then point to next node to faster node
22+
*/
23+
public ListNode removeNthFromEnd(ListNode head, int n) { //n is the gap between the 2 nodes slow and fast
24+
ListNode start= new ListNode(0);
25+
ListNode slow= start;
26+
ListNode fast= start;
27+
28+
//Move fast in front so that the gap between slow node and fast node becomes n
29+
for(int i=1; i<=n+1; i++){
30+
fast=fast.next;
31+
}
32+
33+
//Move fast to the end, maintaining the gap
34+
while(fast != null){
35+
slow= slow.next;
36+
fast= fast.next;
37+
}
38+
39+
//skip the desired node
40+
slow.next= slow.next.next;
41+
return start.next;// returns every node in the list besides deleted node
42+
}
43+
}

‎LeetCode/Lt142.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class Lt142 {
2+
public static void main(String[] args) {
3+
4+
}
5+
6+
public static ListNode detectCycle(ListNode head) {
7+
ListNode slow= head;
8+
ListNode fast= head;
9+
boolean isLoop= false;
10+
11+
if (fast == null || fast.next == null) return null;
12+
13+
while(fast != null && fast.next != null){
14+
fast= fast.next.next;
15+
slow= slow.next;
16+
if(slow == fast){
17+
isLoop=true;
18+
break;
19+
}
20+
}
21+
22+
if(isLoop){
23+
slow=head;
24+
while(slow != fast){
25+
slow= slow.next;
26+
fast=fast.next;
27+
}
28+
29+
return fast;
30+
}
31+
32+
return null;
33+
}
34+
35+
}

‎LeetCode/Lt160.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Given the heads of two singly linked-lists headA and headB,
3+
* return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
4+
* INTERSECTION OF 2 LINKED LISTS
5+
*/
6+
7+
public class Lt160 {
8+
public static void main(String[] args) {
9+
10+
}
11+
12+
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
13+
ListNode a = headA, b = headB;
14+
while (a != b) {
15+
a = a == null ? headB : a.next;
16+
b = b == null ? headA : b.next;
17+
}
18+
return a;
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /