[leetcode]206. Reverse Linked List反转链表

Reverse a singly linked list.

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.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur = head;
ListNode pre = null;
while(cur!= null){
ListNode temp = cur.next;
cur.next = pre;
cur = temp;
}
}
}
上一篇:Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)


下一篇:C++中Cstring、wstring 和string互相转换总结