#Leetcode# 206. Reverse Linked List
https://leetcode.com/problems/reverse-linked-list/
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
代码:
/** * 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) { ListNode *revhead = NULL; ListNode *pnode = head; ListNode *pre = NULL; while(pnode) { if(!pnode -> next) revhead = pnode; ListNode *nxt = pnode -> next; pnode -> next = pre; pre = pnode; pnode = nxt; } return revhead; } };
反转链表 一会粗去转一圈吹吹风 有 1.. 心情不好 很迷茫啊
FHFHFH