Leetcode Daily Questions on Linked List (1) The kth node from the bottom of the linked list, delete the node of the linked list, and print the linked list from end to beginning


Preface

Baidu Encyclopedia: A linked list is a non-contiguous, non-sequential storage structure on a physical storage unit. The logical order of data elements is achieved through the link order of pointers in the linked list. The linked list is composed of a series of nodes (each element in the linked list is called a node), which can be dynamically generated at runtime. Each node includes two parts: one is the data field for storing data elements, and the other is the pointer field for storing the address of the next node. Compared with the linear table sequence structure, the operation is complicated. Because it does not have to be stored in order, the linked list can reach O(1) complexity when inserting, which is much faster than another linear list sequence table, but it takes O(n) to find a node or access a node with a specific number. The time complexity of linear table and sequence table are O(logn) and O(1) respectively.

1. The kth node from the bottom in the linked list

Insert picture description here

Title description

Input a linked list and output the kth node from the bottom of the linked list. In order to conform to the habits of most people, this question starts counting from 1, that is, the end node of the linked list is the first node from the bottom. For example, a linked list has 6 nodes. Starting from the head node, their values ​​are 1, 2, 3, 4, 5, and 6. The third node from the bottom of this linked list is the node with the value 4.
Example:
Given a linked list: 1->2->3->4->5, and k = 2.
Return to the linked list 4->5.
This question is relatively simple to understand, it is easy to achieve 100%, but if you can’t think of it It's not easy to do.
My idea is to construct two pointers, and when the first pointer reaches the Kth value, the second pointer starts to set off. The distance between the two pointers is always K. Then, when the first one reaches the tail, the second pointer just reaches the kth node from the bottom.

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
The process is probably like this

class Solution {
    
    
    public ListNode getKthFromEnd(ListNode head, int k) {
    
    
         ListNode fast = head;//前面的指针
         ListNode slow = head;//后面的指针
         for(int i=0;i<k-1;i++){
    
    
             fast = fast.next;
         }
         while(head.next!=null){
    
    
             fast = fast.next;
             slow = slow.next;
         }
        return slow;
    }
}

Two, delete the node of the linked list

Insert picture description here

Title description

Given the head pointer of the singly linked list and the value of a node to be deleted, define a function to delete the node. Return the head node of the deleted linked list.
Note: This question has been changed compared to the original question.
Example 1:
Input: head = [4,5,1,9], val = 5
Output: [4,1,9]
Explanation: Given the value of 5 in your linked list Two nodes, then after calling your function, the linked list should become 4 -> 1 -> 9.
Example 2:
Input: head = [4,5,1,9], val = 1
Output: [4, 5,9]
Explanation: Given the third node in your linked list whose value is 1, then after calling your function, the linked list should become 4 -> 5 -> 9. Problem
analysis : the idea and above of this problem The idea of ​​this question is similar. The previous one is followed by K bits, and this one is after the ass of the front pointer. When he finds the value to be deleted, he assigns the next bit of the front pointer to the next bit of the back pointer. .
Code:

class Solution {
    
    
    public ListNode deleteNode(ListNode head, int val) {
    
    
        ListNode thrum = new ListNode(-1);
        thrum.next= head; 
        //定义两个指针
        ListNode headhead =thrum
        ListNode fronthead =thrum.next;
        while(fronthead!=null){
    
    
            if(fronthead.val==val){
    
    
            //查找到需要删除的值
            headhead.next= fronthead.next;
            return thrum.next;
            }
            else{
    
    
                headhead=headhead.next;
                fronthead=fronthead.next;
            }            
        }
      
        return  thrum.next;

3. Print the linked list from end to end

Insert picture description here

Title description

Enter the head node of a linked list, and return the value of each node from the end to the beginning (return with an array).
Example 1:
Input: head = [1,3,2]
Output: [2,3,1]
Title analysis : My idea is to first traverse the linked list to get the length of the linked list, and then create an array and assign the linked list from the end of the array , The linked list
code is printed from end to beginning :


```java
class Solution {
    
    
    public int[] reversePrint(ListNode head) {
    
    
         int i = 0;
         ListNode headtwo = head;
         //遍历链表
        while(head!=null){
    
    
            head=head.next;
            i++;
        }
        int[] sites = new int[i];
        //从后往前为数组赋值
        for(int t =i-1;t>=0;t--){
    
    
            sites[t]=headtwo.val;
            headtwo=headtwo.next;
        }
        return sites;
    }
}

Question source: https://leetcode-cn.com/problemset/all/

to sum up

In general, these questions are relatively simple and suitable for novices. The simple questions of the linked list are simpler than the simple questions of the array.

Guess you like

Origin blog.csdn.net/tan45du_yuan/article/details/108684464