Skip to content

Instantly share code, notes, and snippets.

View JamesJi9277's full-sized avatar

Qi Ji JamesJi9277

  • Okta
  • San Francisco, CA
View GitHub Profile
//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;
//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)
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;
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;
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;
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;
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;
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;
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;
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;