[Top 101 must-do interviews] Merge k sorted linked lists & determine whether there is a cycle in the linked list

Table of contents

Topic: Merge k sorted linked lists_NiukeTiba_Niuke.com (nowcoder.com)

Question interface:

Problem-solving ideas:

Code:

Passed! ! !

Topic: Determine whether there is a cycle in the linked list_Niuke Topic_Niuke.com (nowcoder.com)

Question interface:

Problem-solving ideas:

Code:

Passed! ! !

Write at the end:


Topic: Merge k sorted linked lists_NiukeTiba_Niuke.com (nowcoder.com)

Question interface:

package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param lists ListNode类一维数组 
 * @return ListNode类
*/
func mergeKLists( lists []*ListNode ) *ListNode {
    // write code here
}

Problem-solving ideas:

The part of merging linked lists in this question is exactly the same as yesterday. Just implement a merge method call directly. The core idea of ​​this question is the divide and conquer idea. Use the divide and conquer idea to merge all linked lists. The specific operations are as follows:

1) When the number of linked lists == 0, it proves that there is no linked list that needs to be merged, and nil is returned.

2) When the number of linked lists == 1, it proves that there is only one linked list left, and the only linked list is returned directly.

3) When the number of linked lists == 2, it proves that there are only two linked lists left, and the merge of the two linked lists is returned.

4) When the number of linked lists > 2, we use the idea of ​​​​divide and conquer, calculate these linked lists in half, and keep dividing and conquering recursively until the number of linked lists <= 2, so that we can complete each part by following our three logics above Linked lists are merged. code show as below:

Code:

package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param lists ListNode类一维数组 
 * @return ListNode类
*/
func mergeKLists( lists []*ListNode ) *ListNode {
    n := len(lists)

    if n == 0 {
        return nil
    }
    if n == 1 {
        return lists[0]
    }
    if n == 2 {
        return merge(lists[0], lists[1])
    } 

    tmp := n / 2
    return merge(mergeKLists(lists[:tmp]), mergeKLists(lists[tmp:]))
    
}

func merge(list1 *ListNode, list2 *ListNode) *ListNode {
    if list1 == nil && list2 == nil {
        return nil
    }

    head := &ListNode{}
    cur := head
    for list1 != nil && list2 != nil {
        if list1.Val < list2.Val {
            cur.Next = list1
            list1 = list1.Next
        } else {
            cur.Next = list2
            list2 = list2.Next
        }
        cur = cur.Next
    }

    if list1 != nil {
        cur.Next = list1
    }

    if list2 != nil {
        cur.Next = list2
    }

    return head.Next
}

Passed! ! !

Topic: Determine whether there is a cycle in the linked list_Niuke Topic_Niuke.com (nowcoder.com)

Question interface:

package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 
 * @param head ListNode类 
 * @return bool布尔型
*/
func hasCycle( head *ListNode ) bool {
    // write code here
}

Problem-solving ideas:

This question is very, very classic. I have done it many, many times. I still remember my thinking when I first did this question. My idea at the time was to directly forcibly traverse. If the traversal reaches nil, it will be proved. There is no cycle in this linked list. If it loops infinitely beyond the use case length given in the question, it proves that there is no cycle. Don't say it, you have passed it before.

Of course, when I write this question now, I use the standard way of writing fast and slow pointers. The slow pointer takes one step at a time, and the fast pointer takes two steps at a time. If the linked list has a ring, they will meet sooner or later. code show as below:

Code:

package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 
 * @param head ListNode类 
 * @return bool布尔型
*/
func hasCycle( head *ListNode ) bool {
    slow := head
    fast := head
    for fast != nil && fast.Next != nil {
        slow = slow.Next
        fast = fast.Next.Next
        if slow == fast {
            return true
        }
    }
    return false
}

Passed! ! !

Write at the end:

That’s the content of this article, thank you for reading.

If you feel you have gained something, you can give the blogger a like .

If there are omissions or errors in the content of the article, please send a private message to the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/132854053