leetcode206:反转链表
生活随笔
收集整理的這篇文章主要介紹了
leetcode206:反转链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:題目
二:上碼
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/ class Solution { public:ListNode* reverseList(ListNode* head) {ListNode *temp;//存放臨時結點ListNode *cur = head;ListNode *pre = NULL;while(cur){//最后cur指向空為止temp = cur->next;//temp存放cur的下一個結點cur->next = pre;//進行反轉pre = cur;//往后移動一個結點cur = temp;}return pre;} };總結
以上是生活随笔為你收集整理的leetcode206:反转链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode707:设计链表(增删差
- 下一篇: leetcode24. 两两交换链表中的