链表 - Reverse Linked List
Reverse Linked List
描述:
Reverse a singly linked list.反转一个单链表
思路:
1、交换两个节点,并记录两个节点的后一个节点
算法:
/**
* 反转当前链表
* 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 preHead = head;
ListNode CurNode = null;
while (head!= null){
preHead = head.next;
head.next = CurNode;
CurNode = head;
head = preHead;
}
return CurNode;
}
}
转载自: csdn博客 并添加了图解