Niuke Net exam questions

Topic: Input a linked list, after reversing the linked list, output the header of the new linked list.

Reflection summary: Note that if pHead is an empty table, then Phead->next does not exist, so be careful! ! !

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
    
    
public:
    ListNode* ReverseList(ListNode* pHead) {
    
    
        ListNode* L=nullptr;   //运用前插法
        //在这里玩意PHead是空的情况下,那么就没有pHead->next存在!!!注意!!!
        ListNode* p=pHead;
        ListNode* q;
        while(p!=nullptr)
        {
    
    
            q=p->next;
            p->next=L;
            L=p;
            p=q;
        }
        return L;
    }
};

In the second question, a set of k reversed linked lists, and the remaining less than k remain the same.

(1) Use the pre-interpolation method

Flip each group of nodes in the given linked list \kk, and return the flipped linked list.
If the number of nodes in the linked list is not a multiple of \kk, keep the last remaining nodes as they are.
You cannot change the values ​​in the nodes, only The node itself can be changed.
Requires space complexity \ O(1) O(1)
For example:
the given linked list is 1→2→3→4→5
For k=2, you should return 2→1→4→3→5
for k=3 , You should return 3→2→1→4→5

1. The idea of ​​this question: first look at whether there are at least k nodes starting from the current p. Remember to save the position of the original pointer before the loop so that it can be restored at that time.
2. Then remember to save the last node after every k flips (that is, the first node in the original k, which can be linked to the next k-linked list or the remaining links next time).
3. The overall is not difficult, but remember, when p is already nullptr, you can no longer p->next, this is really easy to make mistakes.

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
class Solution {
    
    
public:
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* reverseKGroup(ListNode* head, int k) {
    
    
        int num=0;
        ListNode* L=nullptr;
        ListNode* p=head;
        ListNode* q;
        ListNode* tail=nullptr;
        ListNode* tailpre=nullptr;
        while(p!=nullptr)
        {
    
    
            q=p;     //记录初始结点
            ListNode * start=p;
            num=0;
            while(num<k&&p!=nullptr)
            {
    
    
                num++;
                p=p->next;
            }
            if(num==k)    //这部分构成一个反置链表
            {
    
    
                p=q;
                ListNode *L1=nullptr;
                while(num>0&&p!=nullptr)
                {
    
    
                    if(num==k)
                    {
    
    
                        tail=p;   //保留当前子链表排好序后最后一个元素。
                    }
                    q=p->next;
                    p->next=L1;
                    L1=p;
                    p=q;
                    num--;
                }
                if(L==nullptr)
                {
    
    
                    L=L1;
                }
                else
                {
    
    
                    tailpre->next=L1;
                }
                tailpre=tail;
            }
            else if(num<k)
            {
    
    
                if(tail==nullptr)  //整体长度小于k。
                {
    
    
                    L=start;
                }
                else
                {
    
    
                    tailpre->next=start;  //最后剩余少于k的那部分。
                }
            }
        }
        return L;
        // write code here
    }
};

(2) Use stack

import java.util.*;
import java.math.*;
/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    
    
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public ListNode reverseKGroup (ListNode head, int k) {
    
    
        // write code here
        Stack<ListNode> stack=new Stack<ListNode>();
        ListNode ret=new ListNode(0);
        ListNode p=ret;
        ListNode temp;
        while(true)
        {
    
    
            int num=0;
            temp=head;
            while(num<k&&temp!=null)
            {
    
    
                stack.push(temp);
                num++;
                temp=temp.next;
            }
            if(num<k)
            {
    
    
                p.next=head;
                break;
            }
            else if(num==k)
            {
    
    
                while(!stack.isEmpty())
                {
    
    
                    p.next=stack.pop();
                    p=p.next;
                }
            }
            p.next=temp;
            head=temp;
        }
        return ret.next;
    }
}

Guess you like

Origin blog.csdn.net/qq_43978754/article/details/115215199