剑指offer题目详细版本(2)

递归 版本


/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null){
            return list2;
        }
        
        if(list2==null){
            return list1;
        }
        
        ListNode newHead = null;
        if(list1.val>=list2.val){
            newHead = list2;
            newHead.next = Merge(list1,list2.next);
        }else{
            newHead = list1;
            newHead.next = Merge(list1.next,list2);
        }
        return newHead;
        
    }
}


4、两个链表的第一个公共结点


///版本一:常规方法

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if(pHead1==null||pHead2==null){
            return null;
        }
       
        
        ListNode node=null;
        int length1=0;
        int length2=0;
        int len=0;
        ListNode ppHead1=pHead1;
        ListNode ppHead2=pHead2;
        while(ppHead1!=null){
            length1++;
            ppHead1=ppHead1.next;
        }
        
        while(ppHead2!=null){
            length2++;
            ppHead2=ppHead2.next;
        }
        
        if(length1>=length2){
            len=length1-length2;
            while(len>0){
                pHead1=pHead1.next;
                len--;
            }

        }else if(length1<length2){
            len=length2-length1;
            while(len>0){
                pHead2=pHead2.next;
                len--;
            }
            
        }
       
        
        while(pHead1!=null&&pHead2!=null){
            if(pHead1.val==pHead2.val)
                  return pHead1;
            pHead1=pHead1.next;
            pHead2=pHead2.next;
        }
        return node;
    }
}

版本二 用Map的containKey()的性质
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.HashMap;
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        /用HashMap的containKeys的特性
        ListNode currentpHead1=pHead1;
        ListNode currentpHead2=pHead2;
        
        HashMap<ListNode,Integer> map=new HashMap<ListNode,Integer>();
        while(currentpHead1!=null){
            map.put(currentpHead1,null);
            currentpHead1=currentpHead1.next;
        }
        
        while(currentpHead2!=null){
            if(map.containsKey(currentpHead2)){
                return currentpHead2;
            }
            currentpHead2=currentpHead2.next;
        }
        
        return null;
    }
}


/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.Stack;
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode node = null;
        if(pHead1==null||pHead2==null){
            return node;
        }
        Stack<ListNode> stack1 = new Stack<>();
        Stack<ListNode> stack2 = new Stack<>();
        while(pHead1!=null){
            stack1.push(pHead1);
            pHead1=pHead1.next;
        }
        while(pHead2!=null){
            stack2.push(pHead2);
            pHead2=pHead2.next;
        }
        while(!stack1.empty()&&!stack2.empty()){
            if(stack1.peek().val==stack2.peek().val){
                node = stack1.peek();
                stack1.pop();
                stack2.pop();
            }
            else{
                return node;
            }
        }
        return node;
    }
}


5、链表中环的入口结点

6、链表中倒数最后k个结点

7、复杂链表的复制

8、删除链表中重复的结点

9、删除链表的节点

上一篇:嵌入式模拟智能为机器人提供了新的自主水平


下一篇:如何立即解锁物联网价值