Java code
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode Node = new ListNode(0);
ListNode temp1 = head;
ListNode temp2 = Node;
ListNode temp3 = null;
while(temp1 != null) {
temp2.next = temp1;
temp1 = temp1.next;
temp2.next.next = temp3;
temp3 = temp2.next;
}
return Node.next;
}
}