题目:
Reverse a singly linked list.
实现算法:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){
return null;
}
ListNode currentNode = head;
ListNode preNode = head;
ListNode nextNode = null;
if(head.next!=null){
currentNode = head.next;
while(currentNode.next!=null){
nextNode = currentNode.next;
currentNode.next = preNode;
preNode = currentNode;
currentNode = nextNode;
}
currentNode.next = preNode;
head.next = null;
}
return currentNode;
}
}