LeetCode题组:第206题-反转链表
生活随笔
收集整理的這篇文章主要介紹了
LeetCode题组:第206题-反转链表
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.題目
難度:簡(jiǎn)單
反轉(zhuǎn)一個(gè)單鏈表。
示例:
輸入: 1->2->3->4->5->NULL
輸出: 5->4->3->2->1->NULL
2.我的解答
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* reverseList(struct ListNode* head){if (!head || !head->next) return head;struct ListNode *p=head;struct ListNode* q=head->next;struct ListNode* r=head->next->next;p->next=NULL;while(q){q->next=p;p=q;q=r;if(r) r=r->next;}head=p;return head; }總結(jié)
以上是生活随笔為你收集整理的LeetCode题组:第206题-反转链表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LeetCode题组:第169题-多数元
- 下一篇: LeetCode题组:第543题-二叉树