【面試算法題】有序鏈表基礎介紹,典型面試題講解

有序鏈表的構造

class ListNode{
    int val;
    ListNode nextNode;
        // 構造函數
    ListNode(int val){
        this.val=val;
        this.nextNode=null;
    }
}

    public static ListNode buildListNode(int [] list){
                 //創建3個臨時的ListNode
        ListNode first=null,last=null,newNode;
        for(int i=0;i<list.length;i++){
            newNode=new ListNode(list[i]);
            if(first==null){
                first=newNode;
                last=newNode;
            }else{
                last.nextNode=newNode;
                last=newNode;
            }
        }
        return first;
    }

有序鏈表的簡單打印:

int[] a=new int[]{1,5,6};
ListNode alist=buildListNode( a);

        ListNode testnode = alist;
        while(testnode != null) {
            System.out.println("-->" + testnode.val);
            testnode=testnode.nextNode;
        }
-->1-->5-->6

面試題1:

將兩個有序鏈表合併爲一個新的有序鏈表並返回。新鏈表是通過拼接給定的兩個鏈表的所有節點組成的。
示例:
輸入:1->2->4, 1->3->4
輸出:1->1->2->3->4->4

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

        if (l1 == null) return l2;
        if (l2 == null) return l1;

        ListNode head = null;
        if (l1.val <= l2.val){
            head = l1;
            head.next = mergeTwoLists(l1.next, l2);
        } else {
            head = l2;
            head.next = mergeTwoLists(l1, l2.next);
        }
        return head;

    }
}

面試題2:

給出兩個鏈表3->1->5->null 和 5->9->2->null,返回8->0->8->null

    public static ListNode addList(ListNode list1,ListNode list2){
        ListNode pre=null;
        ListNode last=null,newNode=null;
        ListNode result=null;
        int val=0;
        int carry=0;
        while(list1!=null||list2!=null){
            val=((list1==null?0:list1.val)+(list2==null?0:list2.val)+carry)%10;
            carry=((list1==null?0:list1.val)+(list2==null?0:list2.val)+carry)/10;
            list1=list1==null?null:list1.nextNode;
            list2=list2==null?null:list2.nextNode;  
            newNode=new ListNode(val);
            if(pre==null){
                pre=newNode;
                last=newNode;
            }else{
                last.nextNode=newNode;
                last=newNode;
            }

        }
        if(carry>0){
            newNode=new ListNode(carry);
            last.nextNode=newNode;
            last=newNode;
        }
        return pre;

    }

面試題3:

刪除鏈表中等於給定值val的所有節點。
樣例:
給出鏈表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回刪除3之後的鏈表:1->2->4->5。

思路:
1.首先判斷list1是不是空,爲空就直接返回null
2.然後從list1.next開始循環遍歷,刪除相等於val的元素 ;
----------刪除是怎麼實現的??
【面試算法題】有序鏈表基礎介紹,典型面試題講解
原理是:
(1)node1的nextnode 變爲 node2的nextnode;
(2)node2的變爲原node2的nextnode

3.最後判斷list1的頭部元素是否和val相等,若相等,list1 = list1.next
(這裏最後判斷list1的頭部元素是有原因的,因爲list1的頭部元素只是一個節點,只要判斷一次,如果最先判斷list1的頭部元素就比較麻煩,因爲如果等於val,list1的頭部元素就要發生變化)

代碼:

    public static ListNode  removeList(ListNode list1, int val){
        if(list1 == null) {
            return null;
        }
        ListNode listNode1=list1;
        ListNode listNode2=list1.nextNode;

        while(listNode2 !=null){      
            if(listNode2.val == val) {
                listNode1.nextNode=listNode2.nextNode;
                listNode2=listNode2.nextNode;
            } else {
                listNode1=listNode1.nextNode;
                listNode2=listNode2.nextNode;
            }
        }
        if(list1.val == val) {
            return list1.nextNode;
        }
                return list1;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章