Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Problem1 {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;

while(curr != null){
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}

return prev;
}
}
37 changes: 37 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Problem2 {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;

ListNode slow = dummy;
ListNode fast = dummy;

int count = 0;

while(count <= n){
fast = fast.next;
count++;
}

while(fast != null){
slow = slow.next;
fast = fast.next;
}

ListNode temp = slow.next;
slow.next = slow.next.next;
temp.next = null;

return dummy.next;
}
}
40 changes: 40 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Problem3 {
public ListNode detectCycle(ListNode head) {
if(head == null) return null;
ListNode slow = head;
ListNode fast = head;

boolean flag = false;

while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;

if(slow == fast){
flag = true;
break;
}
}

if(!flag) return null;

slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}

return slow;
}
}