Skip to content

Latest commit

 

History

History
39 lines (33 loc) · 652 Bytes

LinkedList.md

File metadata and controls

39 lines (33 loc) · 652 Bytes

Reverse single linke list

Iterative

class Solution {
	
	public ListNode reverseList(ListNode head) {
		ListNode curr = head;
		ListNode prev = null;
		
		while(curr != null) {
			ListNode next = curr.next;
			curr.next = prev;
			prev = curr;
			curr = next;
		}
		return prev;
	}
}

Recursive

class Solution {

	public ListNode reverseList(ListNode head) {
		return reverse(head, null);
	}
	
	private ListNode reverse(ListNode head, ListNode newHead) {
		if (head == null) return newHead;
		
		ListNode next = head.next;
		head.next = newHead;
		newHead = head;
		head = next;
		return reverse(head, newHead);
	}
}