【面试算法题】有序链表基础介绍,典型面试题讲解

有序链表的构造

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;
        }
    }

转载于:https://blog.51cto.com/devops2016/2310784

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值