Algorism face questions one byte beating

A few days ago a friend to interview bytes beating, the interviewer asked him a list related algorithms questions, but he had not done among out, they came and asked me, I feel pretty good this question, take in terms of a talk.

topic

This is actually a distortion of the reverse problem list, generally described as follows

Given a single head of the list head node, to achieve an adjustment function of a single linked list, such as a set of reverse performed every K between nodes, and starts from the end of the list of groups , not enough number of remaining nodes of the head of a group of We need to reverse. (Or stack can not be used as an auxiliary queue)

For example:
list: 1-> 2-> 3-> 4- > 5-> 6-> 7-> 8-> null, K = 3. Then 6-> 7-> 8,3-> 4-> 5,1> 2 set. Adjusted: 1-> 2-> 5-> 4- > 3-> 8-> 7-> 6-> null. Wherein 1 is not adjusted, since not a group.

answer

This question difficulty is that, from the end of the list is set from the beginning, not from the list of the head , if the head, then we are still relatively easy to do, because you can traverse the list, k to a traversal of each It is split into a group to reverse. But from the end of words is not the same, because it is a single chain, can not traverse the group back together. But this question is certainly better to do recursive, written before the recommendations do not quite understand recursion to see my article Why you can not learn recursion? Farewell recursion, to talk about some of my experiences , the article wrote some about recursive routines.

Do first a similar reversal title

Before solving the problem, we do not imitate take a look if you start from the head of the group since then , how should I do it? For example: list: 1-> 2-> 3-> 4- > 5-> 6-> 7-> 8-> null, K = 3. Adjusted: 3-> 2-> 1-> 6- > 5-> 4-> 7-> 8-> null. Wherein 7,8 is not adjusted, since not a group.

For this question, if you do not know how to reverse a singly-linked list, you can look at what I wrote before how elegant the reverse single list

We use this question can be implemented recursively, assuming method reverseKNode () function is the reverse order (from node K between each single list head starts from the group oh); Function reverse () method is a single list in reverse order.

Then for the following single linked list, where K = 3.


We split up the front and back of the K nodes nodes:


The remaining list of temp point, can be said to be a sub-issue of the original problem. We can call reverseKNode () method will reverse between each node K temp points to the list. Then call reverse () method that the head points to the node 3 is performed in reverse order, as follows:

Again, if this recursion can not quite understand, the proposed recursive thing I read the article I look

Then, we just need to connect the two parts to it. The final results were as follows:

code show as below:

    //k个为一组逆序
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode temp = head;
        for (int i = 1; i < k && temp != null; i++) {
            temp = temp.next;
        }
        //判断节点的数量是否能够凑成一组
        if(temp == null)
            return head;

        ListNode t2 = temp.next;
        temp.next = null;
        //把当前的组进行逆序
        ListNode newHead = reverseList(head);
        //把之后的节点进行分组逆序
        ListNode newTemp = reverseKGroup(t2, k);
        // 把两部分连接起来
        head.next = newTemp;
        
        return newHead;
    }
    
    //逆序单链表
    private static ListNode reverseList(ListNode head) {
        if(head == null || head.next == null)
            return head;
        ListNode result = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return result;
    }

Return to this topic

These two questions can be said to be similar and the only one to start from the head of the group, this group from the road starting from the head, but also leetcode the first 25 questions. The interview, often deformed, such as the Road byte beating title, it becomes the tail began to set on, you may not know how sudden get. Of course, some people may suddenly be reflected, his spike.

其实这道题很好做滴,你只需要先把单链表进行一次逆序,逆序之后就能转化为从头部开始组起了,然后按照我上面的解法,处理完之后,把结果再次逆序即搞定。两次逆序相当于没逆序。

例如对于链表(其中 K = 3)


我们把它从尾部开始组起,每 K 个节点为一组进行逆序。步骤如下

1、先进行逆序


逆序之后就可以把问题转化为从头部开始组起,每 K 个节点为一组进行逆序。

2、处理后的结果如下

3、接着在把结果逆序一次,结果如下

代码如下

public ListNode solve(ListNode head, int k) {
    // 调用逆序函数
    head = reverse(head);
    // 调用每 k 个为一组的逆序函数(从头部开始组起)
    head = reverseKGroup(head, k);
    // 在逆序一次
    head = reverse(head);
    return head;
    
}

类似于这种需要先进行逆序的还要两个链表相加,这道题字节跳动的笔试题也有出过,如下图的第二题

这道题就需要先把两个链表逆序,再节点间相加,最后在合并了。

总结

关于链表的算法题,在面试的时候听说是挺常考的,大家可以多注意注意,遇到不错的链表算法题,也欢迎扔给我勒。

如果你觉得这篇内容对你挺有启发,为了让更多的人看到这篇文章:不妨

1、点赞,让更多的人也能看到这篇内容(收藏不点赞,都是耍流氓 -_-)

2、关注我和专栏,让我们成为长期关系

3、关注公众号「苦逼的码农」,主要写算法、计算机基础之类的文章,里面已有100多篇原创文章

公众号主页
大部分的数据结构与算法文章被各种公众号转载相信一定能让你有所收获

我也分享了很多视频、书籍的资源,以及开发工具,欢迎各位的关注我的公众号:苦逼的码农,第一时间阅读我的文章。

Guess you like

Origin www.cnblogs.com/kubidemanong/p/11334017.html