链表相关面试算法题 java实现

package link;

import java.util.Stack;

public class LinkList {

private Node head;

private Node current;

class Node{

int data;

Node next;

public Node(int data){

this.data = data;

}

}

//链表的添加

public void add(int data){

if(head == null){

head = new Node(data);

current = head;

}else{

current.next = new Node(data);

current = current.next;

}

}

//链表的打印

public void print(Node node){

if(node == null){

return;

}

current = node;

while(current != null){

System.out.println(current.data);

current = current.next;

}

}

public static void main(String[] args){

LinkList list = new LinkList();

for(int i=0;i<10;i++){

list.add(i);

}

//
list.print(list.head);

//
System.out.println(“length = “+list.getLength(list.head));

//
System.out.println(“mid = “+list.findMidNode(list.head).data);

LinkList list1 = new LinkList();

        LinkList list2 = new LinkList();

        //向LinkList中添加数据

        for (int i = 0; i < 4; i++) {

            list1.add(i);

        }

        for (int i = 0; i < 8; i++) {

            list2.add(i);

        }

        LinkList list3 = new LinkList();

        list3.head = list3.mergeLinkList(list1.head, list2.head); //将list1和list2合并,存放到list3中

        list3.print(list3.head);// 从head节点开始遍历输出

}

//获取链表的长度

private int getLength(Node head){

if(head == null){

return 0;

}

int length = 0;

Node current = head;

while(current != null){

length ++;

current = current.next;

}

return length;

}

public int findLastNode(Node head,int index){

if(head == null){

return -1;

}

int size = 0;

current = head;

while(current != null){

size++;

current = current.next;

}

current = head;

for(int i=0;i<size – index ;i++){

current = current.next;

}

return current.data;

}

private Node findLastNode2(Node head , int k){

if(k == 0 || head == null){

return null;

}

Node q = head;

Node h = head;

for(int i=0;i<k-1;i++){

h = h.next;

if(h == null){

return null;

}

}

while(h.next != null){

q = q.next;

h = h.next;

}

return q;

}

//查找中间节点

private Node findMidNode(Node head){

if(head == null){

return null;

}

Node q = head;

Node h = head;

while(h != null && h.next != null){

q = q.next;

h = h.next.next;

}

return q;

}

private Node mergeLinkList(Node head1,Node head2){

if(head1 == null && head2 == null){

return null;

}

if(head1 == null){

return head2;

}

if(head2 == null){

return head1;

}

Node head;

Node current;

if(head1.data < head2.data){

head = head1;

current = head1;

head1 = head1.next;

}else{

head = head2;

current = head2;

head2 = head2.next;

}

while(head1 != null && head2 != null){

if(head1.data < head2.data){

current.next = head1;

current = current.next;

head1 = head1.next;

}else{

current.next = head2;

current = current.next;

head2 = head2.next;

}

}

if(head1 != null){

current.next = head1;

}

if(head2 != null){

current.next = head2;

}

return head;

}

//链表反转

private Node reverseList(Node head){

if(head == null || head.next == null){

return head;

}

Node current = head;

Node next = null;

Node reverseHead = null;

while(current != null){

next = current.next;

current.next = reverseHead;

reverseHead = current;

current = next;

}

return reverseHead;

}

//从尾到头打印单链表

private void reversePrint(Node head){

if(head == null){

return;

}

Stack<Node> stack = new Stack<Node>();

Node current = head;

while(current != null){

stack.push(current);

current = current.next;

}

while(stack.size() > 0){

System.out.println(stack.pop().data);

}

}

private void reversePrint2(Node head){

if(head == null){

return;

}

reversePrint2(head.next);

System.out.println(head.data);

}

//判断链表是否有环

private boolean hasCycle(Node head){

if(head == null){

return false;

}

Node q = head;

Node h = head;

while(h != null){

q = q.next;

h = h.next.next;

if(q == h){

return true;

}

}

return false;

}

//链表有环时 相遇的节点

private Node hasCycle2(Node head){

if(head == null){

return null;

}

Node q = head;

Node h = head;

while(q != null){

q = q.next.next;

h = h.next;

if(q == h){

return q;

}

}

return null;

}

private int getCycleLenngth(Node node){

if(head == null){

return 0;

}

Node current = node;

int length = 0;

while(current != null){

current = current.next;

length++;

if(current == node){

return length;

}

}

return length;

}

//有环的链表 起始位置 

private Node getCycleStart(Node head,int cycleLength){

if(head == null){

return null;

}

Node q = head;

Node h = head;

for(int i = 0; i<cycleLength;i++){

h = h.next;

}

while(q != null &&h != null){

q = q.next;

h = h.next;

if(q == h){

return q;

}

}

return null;

}

//两个单链表相交的第一个节点

private Node getFirstCommonNode(Node head1, Node head2){

if(head1 == null || head2 == null){

return null;

}

int length1 = getLength(head1);

int length2 = getLength(head2);

int lengthDif = 0;

Node longHead;

Node shortHead;

if(length1 > length2){

longHead = head1;

shortHead = head2;

lengthDif = length1 – length2;

} else {

longHead = head2;

shortHead = head1;

lengthDif = length2 – length1;

}

//将较长的那个链表的指针向前走length个距离

for(int i=0;i<lengthDif;i++){

longHead = longHead.next;

}

while(longHead != null && shortHead != null){

if(longHead == shortHead){

return longHead;

}

longHead = longHead.next;

shortHead = shortHead.next;

}

return null;

}

}

    原文作者:蒋帅Android
    原文地址: https://blog.csdn.net/j18874964028sss/article/details/78204645
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞