作业帮面试算法

两个链表相交问题

这道题目可以拆为三个子问题

  • 如何判断一个链表是否有环,如果有,则返回第一个进入环的节点,没有则返回null
  • 如何判断两个无环链表是否相交,相交则返回第一个相交节点,不想交则返回nll
  • 如何判断两个有环链表是否相交,如果有,则返回第一个进环节点,没有则返回null
判断是否有环
public class GetLoopNode(Node head){
    if(head==null || head.next == null|| head.next.next == null)
        return null;
    Node n1 = head.next; // n1 -> slow
    Node n2 = head.next.next; // s2 -> fats
    while( n1 != n2){
        if (n2.next == null || n2.next.next == null)
            return null;
        n2 = n2.next.next;
        n1 = n1.next;
    }
    n2 = head ; // n2 -> begin
    while(n1 != n2){
        n1 = n1.next;
        n2 = n2.next;
    }
    return n2;
}

判断无环链表相交问题
public Node noloop(Node head1, Node head2){
    if(head1 == null || head2 == null)
        return null;
    Node cur1 = head1;
    Node cur2 = head2;
    int n = 0;
    while( cur1.next != null){
        n ++;
        cur1 = cur1.next;
    }
    while( cur2.next != null){
        n -- ;
        cur2 = cur2.next;
    }
    if( cur1 != cur2){
        return null;
    }
    cur1 = n > 0 ? head1, head2; // 长
    cur2 = cur1 == head1?? head2, head1; //短
    n = Math.abs(n);
    while (n != 0){
        n -- ;
        cur1 = cur1.next;
    }
    while (cur1 != cur2){
        cur1 = cur1.next;
        cur2 = cur2.next;
    }
    return cur1;
}
判断有环链表是否相交
// loop1 为list1入环节点, loop2 为list2入环节点
public Node bothLoop(Node head1, Node loop1, Node head2, Node loop2){
     Node cur1 = null;
     Node cur2 = null;
     if(loop1 == loop2){
        cur1 = head1;
        cur2 = head2;
        int n = 0;
        while(cur1 != loop1){
            n++;
            cur1 = cur1.next;
        }
        while(cur2 !=loop2){
            n--;
            cur2 = cur2.next;
        }
        cur1 = n >0 ? head1: head2;
        cur2 = cur1==head1? head2: head1;
        n = Math.abs(n);
        while(n!=0){
            n--;
            cur1 = cur1.next;
        }
        while(cu1 != cur2){
            cur1 =cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
     }else{
        cur1 = loop1.next;
        while(cur1 != loop1){
            if(cur1 == loop2){
                return loop1;
            }
            cur1 = cur1.next;
        }
        return null;
     }
}
综合求解
    public int value;
    public Node next;
    public Node(int data){
        this.val = val;
    }
}

public Node getIntersectNode(Node head1, Node head2){
    if(head1 == null || head2 == null)
        return null;
    Node loop1 = getLoopNode(head1);
    Node loop2 = getLoopNode(head2);
    if(loop1 == null && loop2 == null)
        return noLoop(head1, head2);
    if(loop1 != null && loop2 != null)
        return bothLoop(head1, loop2, head2, loop2);
    return None;
}

反转字符串(注意只让反转单词顺序)

  • 先整体反转
  • 再把各个单词找出来用reverse反转
//  注意这个解法那块求每个单词l,r很鸡贼
public void rotateWord(char[] chars){
    if(chars == null || chars.length == 0){
        return ;
    }
    reverse(chars, 0, chars.length-1);
    int l = -1;
    int r = -1;
    for(int i = 0 ;i

两个链表找相同元素(排序好的链表)

public class Node{
    public int value;
    public Node next;
    public Node(int data){
        this.value = data;
    }
}
public void printCommonPart(Node head1, Node head2){
    while(head1 != null && head2 != null){
        if(head1.value < head2.value){
            head1 = head1.next;
        }else if(head1.value > head2.value){
            head2 = head2.next;
        }else{
            System.out.Println(head1.value + "");
            head1 = head1.next;
            head2 = head2.next;
        }

    }
    System.out.Println();
}

你可能感兴趣的:(作业帮面试算法)