In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Python how to reverse single linked list". In daily operation, I believe many people have doubts about how Python reverses single linked list. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how Python reverses single linked list". Next, please follow the editor to study!
Topic: reverse the single linked list, you can use iterative or recursive methods.
The iterative method, to put it simply, is that when the iteration reaches the deepest level, the address of cur is the same as that of new_head when it is returned. Operating cur is equivalent to operating new_head. Head- > next = NULL is to discard the value that has been returned.
Language:C
Iteratively:
/ * Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode* next; *}; * / struct ListNode* reverseList (struct ListNode* head) {struct ListNode* pre = (struct ListNode*) malloc (sizeof (struct ListNode)); struct ListNode* cur = (struct ListNode*) malloc (sizeof (struct ListNode)); struct ListNode* temp = (struct ListNode*) malloc (sizeof (struct ListNode)); if (head = NULL | head- > next = = NULL) {return head;} pre = head; cur = head- > next Pre- > next = NULL;while (cur! = NULL) {temp = cur- > next; cur- > next = pre; pre = cur; cur = temp;} return pre;}
Recursively:
/ * Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode* next; *}; * / struct ListNode* reverseList (struct ListNode* head) {struct ListNode* cur = (struct ListNode*) malloc (sizeof (struct ListNode)); struct ListNode* new_head = (struct ListNode*) malloc (sizeof (struct ListNode)); if (head = = NULL | head- > next = = NULL) {return head;} / / iterates to the deepest layer, and the address of cur is the same as that of new_head when returned. Operating cur is equivalent to operating new_head. Head- > next = NULL is to discard the value that has been returned. Cur = head- > next; new_head = reverseList (cur); head- > next = NULL; cur- > next = head;return new_head;}
Language: python
# Definition for singly-linked list.# class ListNode (object): # def _ _ init__ (self, x): # self.val = x # self.next = Noneclass Solution (object): def reverseList (self) Head): ": type head: ListNode: rtype: ListNode"pre = Nonewhile head: cur = head head = head.next cur.next = pre pre = curreturn pre so far The study on "how to reverse the single linked list by Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.