思路:用两个链表结点进行一步一步修改就行了。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null)
return null;
ListNode tmp1=null;
ListNode tmp2=head;
while(tmp2.next!=null){
ListNode a1=tmp2.next;
ListNode a2=tmp2;
tmp2.next=tmp1;
tmp2=a1;
tmp1=a2;
}
tmp2.next=tmp1;
return tmp2;
}
}