您的位置:首页 > 编程语言 > C语言/C++

LeetCode-206. Reverse Linked List

2017-03-02 16:59 369 查看
问题:

https://leetcode.com/problems/reverse-linked-list/?tab=Description

Reverse a singly linked list.反转一个单向链表。

参考C++代码:

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL || head->next == NULL)  return head;
ListNode Result(0);
ListNode *p = head;
while (p) {
ListNode *q = p->next;
p->next = Result.next;
Result.next = p;
p = q;
}
return Result.next;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struct leetcode c++