How C++ customizes one-way linked list ListNode
Editor to share with you how C++ customizes the one-way linked list ListNode, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
There are two types of linked lists:
1, the lead node, the head node stores the length of the linked list, and the data is stored from the second node.
2. There is no lead node, no node that stores the length of the linked list, and data is stored from the head node.
The linked list defined here is the second one.
Go directly to the code:
# include # include using namespace std;struct ListNode {int val; / / value of the current node ListNode * next / / pointer to the next node ListNode (): val (0), next (nullptr) {} / / initializes the current node value to the default value of 0, the pointer to null ListNode (int x): val (x), next (nullptr) {} / / initializes the current node value to x, and the pointer to null ListNode (int x, ListNode * next): val (x) Next (next) {} / / initialize the current node value is x, and the next grade point is next} Class Solution {public: / / create an one-way linked list void createList (ListNode * head,int len) with the length of len {for (int inode 1 / ivalidi; / / assign the value node- > next=nullptr; head- > next=node; / / head to the next node (that is, the current node) head=node; / / set the current node to head} cout