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
//is valid palindrome | |
//reverse and compare, time complexity O(n), space complexity O(n) | |
public class Solution1{ | |
public boolean isValidPalindrome(LinkedListNode head){ | |
LinkedListNode l1 = head; | |
LinkedListNode l2 = head; | |
while(l1 != null && l1.next != null) | |
{ | |
LinkedListNode before = new LinkedListNode; | |
before.next = l2; |
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
//Given a circular linked list, implement an algorithm which returns the node at the begining of the loop | |
public class Solution{ | |
public LinkedListNode findBegining(LinkedListNode head){ | |
LinkedListNode slow = head; | |
LinkedListNode fast = head; | |
while(fast != null || fast.next != null) | |
{ | |
slow = slow.next; | |
fast = fast.next.next; | |
if(fast == slow) |
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
public class Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.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
public class Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.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
public class Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.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
public class Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.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
public class Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.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
public class Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.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
public class Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.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
public class Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.next; |
NewerOlder