Niuke 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:

Example 1

enter:

{1,2,3}

Copy return value:

{3,2,1}

copy

Example 2

enter:

{}

Copy return value:

{}

Copy instructions:

Empty linked list outputs empty

Solution 1 (c)

Initialization: 3 pointers
1) The pre pointer points to the last node of the linked list that has been reversed, and there is no reversal at the beginning, so it points to NULL
2) The cur pointer points to the first node of the linked list to be reversed, the first node at the beginning The node is to be reversed, so it points to head
3) The nex pointer points to the second node of the linked list to be reversed, the purpose is to save the linked list, because after cur changes the pointer, the subsequent linked list will become invalid, so it needs to be saved Next, execute the following in a
loop Three operations
1) nex = cur->next, save function
2) cur->next = pre The next pointer of the first node of the unreversed linked list points to the last node of the reversed linked list
3) pre = cur, cur = nex; move the pointer backwards, operate the first node of the next unreversed linked list The
loop condition, of course, is cur != NULL
After the loop ends, cur is of course NULL, so return pre, which is the reversed header point

code:

struct ListNode* ReverseList(struct ListNode* pHead ) {
    // write code here
    
    if (pHead==NULL) return NULL;
    
    struct ListNode *pre=NULL;
    struct ListNode *cur=pHead;
    struct ListNode *nex=NULL;
    
    while(cur){
        nex=cur->next;
        cur->next=pre; //未反转链表的第一个节点的下个指针指向已反转链表的最后一个节点
        pre=cur;
        cur=nex;
    }
    return pre;
}

 Solution 2 (c++)

Construct a linked list, use vector to store first, and then construct

Time complexity: O(n)
Space complexity: O(n), a vector is used to store a single linked list

code:

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        
        if(!pHead) return nullptr;
        vector<ListNode*> v;
        while(pHead){
            v.push_back(pHead);
            pHead=pHead->next;
        }
        reverse(v.begin(), v.end()); //反转vector 或者逆向遍历
        ListNode *head=v[0];
        ListNode *cur=head;
        for(int i=1;i<v.size();i++){ //构造链表
            cur->next=v[i]; //当前节点的下一个指针指向下一个节点
            cur=cur->next;  //当前指针后移
        }
        cur->next=nullptr;  //切记最后一个节点的下一个指针指向nullptr
        return head;
    }
};

Solution 3 (java) 

Using a stack, the stack is first in last out

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

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.Stack;
public class Solution {
    public ListNode ReverseList(ListNode head) {
    Stack<ListNode> stack =new Stack<>();
      //把链表节点全部摘掉放到栈中
   while(head!=null){
       stack.push(head);
       head=head.next;
    }
    if(stack.isEmpty()) return null;
    ListNode node=stack.pop();
    ListNode dummy =node;
    //栈中的结点全部出栈,然后重新连成一个新的链表
    while(!stack.isEmpty()){
       ListNode tempnode=stack.pop();
       node.next=tempnode;
       node=node.next;
    }
    //最后一个结点就是反转前的头结点,一定要让他的next
    //等于空,否则会构成环
    node.next=null;
    return dummy;

    }
}

Guess you like

Origin blog.csdn.net/weixin_53630942/article/details/124136162