标签:post his return name 题目 val 节点 turn reverse
题目描写叙述:
给定一个链表,要求将这个链表进行翻转。
时间O(n),空间O(1)
解题思路:
设定三个指针,temp。 prev,head,从头到尾依次翻转。
head代表当前节点,prev代表前一个节点,temp代表下一个节点。
參考代码:
/** * Definition of ListNode * * class ListNode { * public: * int val; * ListNode *next; * * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ class Solution { public: /** * @param head: The first node of linked list. * @return: The new head of reversed linked list. */ ListNode *reverse(ListNode *head) { // write your code here ListNode* prev = NULL; while(head != NULL) { ListNode* temp = head->next; head->next = prev; prev = head; head = temp; } return prev; } };
标签:post his return name 题目 val 节点 turn reverse
原文地址:http://www.cnblogs.com/cxchanpin/p/6956498.html