Brush up on Niuke's linked list questions, gradually deepen the linked list, and understand the linked list

ced485cbb11e458d81a746890b32cf3f.gif 

Author: Earth Dog Thirsting for Strength

Blog homepage: blog homepage of a power-hungry dirt dog

Column: Take you hand in hand to brush Niu Ke

If a worker wants to do a good job, he must first sharpen his tool. Let me introduce a super awesome tool to win the offer of a big factory——Niuke.com

Click to register for free and brush questions with me

 

Table of contents

1. Reverse linked list

2. Delete the last nth node of the linked list

3. Determine whether a linked list is a palindrome structure


1. Reverse linked list

describe

Given the head node pHead of a singly linked list (the head node has a value, for example, in the figure below, its val is 1), the length is n, after reversing the linked list, return the head of the new linked list.

Data range: 0≤n≤1000

Requirements: space complexity O(1), time complexity O(n).

For example, when entering the linked list {1,2,3},

After inversion, the original linked list becomes {3,2,1}, so the corresponding output is {3,2,1}.

The above conversion process is shown in the figure below:

示例1
输入:
{1,2,3}
返回值:
{3,2,1}



示例2
输入:
{}
返回值:
{}
说明:
空链表则输出空                 

Idea description: In order to reverse the linked list, simply put, it is to modify the next value of the node. If we use cur to record the current node, then when modifying the point, we need to know its previous node ( pre) and the next node (curNext), because once the pointing modification is completed, the subsequent node will not be found, so we need to record it. So we first record the next node of the current node, then point the next of the current node cur to pre, and let the pre move backward, that is, pre=cur; then let cur point to the curNext of the record, until the entire traversal linked list.

  AC code:

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {

        ListNode cur=head;

        ListNode pre=null;

        while(cur!=null){
            ListNode curNext=cur.next;
            cur.next=pre;
            pre=cur;
            cur=curNext;
        }

        return pre;

    }
}

2. Delete the last nth node of the linked list

describe

Given a linked list, delete the last nth node of the linked list and return the head pointer of the linked list
For example, the given linked list is: 1→2→3→4→5, n=2.
Delete the last nth node of the linked list After that, the linked list becomes 1→2→3→5.

Data range: The length of the linked list is 0≤n≤1000, and the value of any node in the linked list satisfies 0≤val≤100

Requirements: Space complexity O(1), time complexity O(n)
Remarks: The title guarantees that n must be valid

示例1
输入:
{1,2},2    

返回值:
{2}

Idea description: First of all, it is necessary to judge the void, otherwise there is no need to proceed. The operation of deleting a node in the linked list is relatively simple, just let the next node of the previous node of the node point to the next node of the node. The key to this question is how to find this node. Since it is reciprocal, we need to calculate the length of the linked list, just write a size function. Then convert the reciprocal into a positive number, define a newSize=size(head)- n; to get the positive node position, and then delete it!

AC code:

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode removeNthFromEnd (ListNode head, int n) {
        if(head==null){
            return null;
        }
        ListNode cur=head;
        ListNode pre =null;
        int newSize=size(head)-n;
        while(cur!=null&&newSize!=0){
            pre=cur;
            cur=cur.next;
            newSize--;
        }
        if(cur!=head){
            pre.next=cur.next;
        }else{
            head=cur.next;
        }


        return head;

    }
    public int size(ListNode head){
        int size=0;
        ListNode cur=head;
        while(cur!=null){
            cur=cur.next;
            size++;
        }
        return size;
    }
}

3. Determine whether a linked list is a palindrome structure

describe

Given a linked list, please determine whether the linked list is a palindrome.

A palindrome means that the string is exactly the same in both forward and reverse order. 

Data range: The number of nodes in the linked list is 0≤n≤10^5, and the value of each node in the linked list satisfies ∣val∣≤10^7

示例1
输入:
{1}

返回值:
true

示例2
输入:
{2,1}

返回值:
false

说明:
2->1     
示例3
输入:
{1,2,2,1}

返回值:
true

说明:
1->2->2->1     

Idea description: We have seen a lot of palindrome numbers. For linked lists, we want to know whether it is a palindrome number, and we often use double pointers to solve it. The key to this question is to find the middle node, and then reverse the nodes behind the middle node. After the reversal, compare them from both ends until there is an inequality. If there is no inequality, it is a palindrome , any unequal is not a palindrome. The main idea is to define a fast pointer and a slow pointer. The fast one takes two steps at a time, and the slow one takes one step at a time. The following nodes are reversed, as mentioned above how to reverse, and finally, start to compare from both ends!

AC code:

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    public boolean isPail (ListNode head) {
        // write code here
          if(head==null){
            return true;
        }
        if(head.next==null){
            return true;
        }
       

        ListNode slow=head;
        ListNode fast=head;

        while(fast!=null&&fast.next!=null){

            fast=fast.next.next;
            slow=slow.next;

        }

        ListNode cur=slow.next;
        while(cur!=null){

            ListNode curNext=cur.next;
            cur.next=slow;
            slow=cur;
            cur=curNext;
        }

        while(head!=slow){
            if(head.val!=slow.val){
                return false;
            }
            if(head.next==slow){
                return true;
            }
            head=head.next;
            slow=slow.next;
        }
        return true;
    }
}

 "This is the end of the sharing of this issue, remember to give the blogger a three-way haha, your support is the biggest motivation for my creation!     

Guess you like

Origin blog.csdn.net/m0_67995737/article/details/127698050