This list algorithm to learn a few questions, interview no longer afraid of a handwritten list

This list algorithm to learn a few questions, interview no longer afraid of a handwritten list

The author writing skill is still shallow, if wrong, please point out the generosity, will be grateful

In the interview are often asked to make handwritten code on the list, a few questions are ever asked me in the interview below. Of course I write is not necessarily the optimal solution, if there is a better way to welcome everyone said.

Help everyone to watch my first title list

  • Delete the list penultimate N nodes
  • Reverse list
  • Merge two ordered lists
  • Seeking intermediate node list

Delete the list penultimate N nodes

Title: Given a list, delete list reciprocal of n nodes, and returns to the head node list.

给定一个链表: 1->2->3->4->5, 和 n = 2.

当删除了倒数第二个节点后,链表变为 1->2->3->5.

复制代码

In the list of topics, sometimes a pointer to solve the problem then we add a pointer. Generally two pointers can solve most of the problems

We define the list above is, for example, we want to remove the penultimate N nodes, then define two pointers, a fast-pointer, a slower pointer. Fast pointer faster than slower pointer N steps, and then move forward with the speed of the pointer, then just went fast pointer Nullwhen the pointer is pointing to is the slow node we want to delete.

For example For example, we want to remove the penultimate node, then initialize the pointer to a pointer as follows.

Traversed after pointer as follows, we can see that the left node pointer points to is that we want to remove

As part of the code

public void deleteNodeForN(Node head, int n){

    Node left = head;
    Node right = head;
    int i = 0;
    while (right!=null && i < n){
        right = right.getNext();
        i++;
    }

    while (right!=null){
        left = left.getNext();
        right = right.getNext();
    }
	// 遍历完后删除左节点
    deleteNode(left);

}

复制代码

Reverse list

Topic: Reverse a singly linked list.

输入: 0->1->2->3->4->5->NULL
输出: 5->4->3->2->1->0->NULL

复制代码

The list is no problem can not be solved by adding a pointer, if there is, then add a pointer.

Solution a: Pointer plus

In the above list to delete the N-th node, we added two pointers to solve the problem, then the next if you want to reverse a linked list of how to do it? Two pointers was not enough, we need three hands to define the current node before the current node, the current node. Of course, this approach is neither space, time is a fast solution.

Or a list of our definition, then we want pointers effect is like? Next we use illustrated step by step demonstration of how to use three pointer list upside down, we do not see the answer to my final solution is given, can try to look at my chart to write the code again, see if I can write about.

Shows part of the code


public Node reversalNodeThree(Node head) {
    if (head == null || head.getNext() == null){
        return head;
    }

    Node preNode = null;
    Node nextNode = null;
    while (head != null){
        nextNode = head.getNext();
        head.setNext(preNode);
        preNode = head;
        head = nextNode;
    }
    return preNode;
}

复制代码

Solution II: recursive

The key code is recursive how large problem into smaller problems of the law, and based on this write recursive formula, and then refine the termination condition.

When writing recursive code, our ideas do not step by step inside cover, set the set of their own will be easier to Mongolia. In fact, the essence of recursive decomposition is small tasks to perform, and our way of thinking correctly masked recursive detail, suppose we want to have the results back, and then just the first step can be.

We have to reverse a linked list as an example, how to use recursive thought to think, and how our thinking becomes code.

Here the next node node 0 is not a 5 node, but node under our large gray background

Or the above list, for example, we want to reverse, assume that the first node then all nodes have successfully reversed, then how do we do next? I believe we will be right here, the equivalent of converting two nodes.

Node(0).getNext().setNext(Node(0));
Node(0).setNext(null);

复制代码
  • Termination condition: when Node.next Node is the mass of the mass is null or null. Show-entered the empty node or to a node, without reversing

We use the above conditions and termination code analysis can write out the following recursive reversing a chain code.

public Node reversalNodeTwo(Node head){

    if (head == null || head.getNext() == null){
        return head;
    }

    Node reHead = reversalNodeTwo(head.getNext());

    head.getNext().setNext(head);

    head.setNext(null);

    return reHead;

}

复制代码

Merge two ordered lists

Title: The two ordered lists into a new sorted list and return. The new list is by all nodes in a given mosaic composed of two lists.

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

复制代码

Iteration

Iterative way merge two lists is to solve the problem by way of a pointer. Join us now have the following two lists. We define two pointers pointing to the header of the two lists to start one by one comparison, the smaller one of the node moved out, the pointer is moved backward. Until it is null . Next we break down each step with pictures. We can encode their first practice at the prompts picture, followed with answers.

Shows part of the code

public Node mergeTwoListTwo(Node nodeOne, Node nodeTwo){

    AboutLinkedList mergeTwoList = new AboutLinkedList();
    Node headNodeOne = nodeOne;
    Node headNodeTwo = nodeTwo;

    while (headNodeOne!=null || headNodeTwo!=null){

        if (headNodeOne == null || headNodeOne.getNum() > headNodeTwo.getNum()){
            mergeTwoList.addNode(headNodeTwo);
            Node pre = headNodeTwo;
            headNodeTwo = headNodeTwo.getNext();
            pre.setNext(null);
        }else {
            mergeTwoList.addNode(headNodeOne);
            Node pre = headNodeOne;
            headNodeOne = headNodeOne.getNext();
            pre.setNext(null);
        }
    }
    return mergeTwoList.head.getNext();
}

复制代码

Seeking intermediate node list

Title: find the list of the intermediate node

输入:0->1->2->3->4
输出:2

复制代码

pointer

In general list of questions we can use the pointer if either time or space are the optimal solution, in fact, there is little skill, is the definition of two pointers (speed indicator), fast pointer each pass two nodes, every time a node pointer to go slow. When the pointer went so fast last time slow pointer just reached the middle position. Next we chart a more intuitive feel for how the pointer to go. You can write the code yourself accordance with the plans of the presentation, and then my last look at the code given. This will be more profound memory

Shows part of the code

public Node getNodeForCenter(Node head){
    if (head == null){
        return null;
    }else if (head.getNext() == null){
        return head;
    }
    Node slow = head;
    Node fast = head;
    while (fast!=null && fast.getNext()!=null){
        slow = slow.getNext();
        fast = fast.getNext().getNext();
    }
    return slow;
}

复制代码

Code address

Guess you like

Origin juejin.im/post/5dd884936fb9a07a9323de6f