###Only one argument needed###
Only one argument needed
First of all, your third argument reverseListHead
should be a local variable because it is never used before being set.
Next, if you change your algorithm slightly, you don't need the second argument either:
public static Node reverseList(Node head)
{
// Empty list or list with one element.
if (head == null || head.next == null) {
return head;
}
Node second = head.next;
Node reversedList = reverseList(second);
// The second node is now the tail of the reversed list,
// so when we append head to it, head becomes the new tail.
second.next = head;
head.next = null;
return reversedList;
}
###Only one argument needed###
First of all, your third argument reverseListHead
should be a local variable because it is never used before being set.
Next, if you change your algorithm slightly, you don't need the second argument either:
public static Node reverseList(Node head)
{
// Empty list or list with one element.
if (head == null || head.next == null) {
return head;
}
Node second = head.next;
Node reversedList = reverseList(second);
// The second node is now the tail of the reversed list,
// so when we append head to it, head becomes the new tail.
second.next = head;
head.next = null;
return reversedList;
}
Only one argument needed
First of all, your third argument reverseListHead
should be a local variable because it is never used before being set.
Next, if you change your algorithm slightly, you don't need the second argument either:
public static Node reverseList(Node head)
{
// Empty list or list with one element.
if (head == null || head.next == null) {
return head;
}
Node second = head.next;
Node reversedList = reverseList(second);
// The second node is now the tail of the reversed list,
// so when we append head to it, head becomes the new tail.
second.next = head;
head.next = null;
return reversedList;
}
###Only one argument needed###
First of all, your third argument reverseListHead
should be a local variable because it is never used before being set.
Next, if you change your algorithm slightly, you don't need the second argument either:
public static Node reverseList(Node head)
{
// Empty list or list with one element.
if (head == null || head.next == null) {
return head;
}
Node second = head.next;
Node reversedList = reverseList(second);
// The second node is now the tail of the reversed list,
// so when we append head to it, head becomes the new tail.
second.next = head;
head.next = null;
return reversedList;
}