92. Reverse Linked List II

Description

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ mn ≤ length of list.

Constraints

Approach

Examples

Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, m = 2, n = 4

Output: 1 -> 4 -> 3 -> 2 -> 5 -> NULL

Solutions

// 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;
	}
}

Follow up

  • Reverse even elements in a Linked List - GFG

Last updated

Was this helpful?