Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently shift a linked list by k positions, in Java?

/*
	This implementation demonstrates how to 
	efficiently shift a singly linked list
	in place by k positions.

	Example: 
	shifting 0-> 1 -> 2 -> 3 -> 4-> null 
	by 2 positions yields: 
	3 -> 4 -> 0 -> 1 -> 2 -> null

	
	Let n be the number of nodes in the list.
	Time complexity: O(n) 
	Space complexity: O(1)
*/
public class LinkedListShifting {
	private ListNode head;

	public LinkedListShifting() {
		/*
		 * Create below linked list
		 * 0 -> 1 -> 2 -> 3 -> 4 -> null
		 */
		head = new ListNode(0, null);
		ListNode prev = head, temp;
		for (int i = 1; i <= 4; i++) {
			temp = new ListNode(i, null);
			prev.next = temp;
			prev = temp;
		}
	}

	public static void main(String[] args) {
		LinkedListShifting application = new LinkedListShifting();
		application.head = application.shiftLinkedList(2);
		System.out.println(application.head.val); // 3
	}

	public ListNode shiftLinkedList(int k) {
		if (head == null) {
			return null;
		}
		int length = 1;
		ListNode listTail = head;
		// Count the number of list nodes
		while (listTail.next != null) {
			listTail = listTail.next;
			length++;
		}
		// The offset should less than length
		int offset = Math.abs(k) % length;
		if (offset == 0)
			return head; // no shifting is needed
		int newTailPosition = k > 0 ? length - offset : offset;
		ListNode newTail = head;
		for (int i = 1; i < newTailPosition; i++) {
			newTail = newTail.next;
		}
		ListNode newHead = newTail.next;
		newTail.next = null;
		listTail.next = head;
		return newHead;
	}

	// Class representing a linked list node
	// with pointers to value and next node
	private class ListNode {
		int val;
		ListNode next;

		ListNode(int val, ListNode next) {
			this.val = val;
			this.next = next;
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to check if the file has remaining without reading from it java 
Java :: measure running time of a statement java 
Java :: enum with numbers java 
Java :: copy constructor in java 
Java :: number of digits java 
Java :: playerhead command minecraft 
Java :: min height bst leetcode 
Java :: check how many times a character appears in a string java 
Java :: java switch case 
Java :: export java_home linux 
Java :: write an infinite loop java 
Java :: how to find the length of a string in java without using length function 
Java :: java get first day of the week 
Java :: java get file name without extension 
Java :: passing array by reference java 
Java :: java type casting 
Java :: java currency format 
Java :: android xml change button background 
Java :: parameterized constructor 
Java :: find minimum number in java 
Java :: how to print in a new line in java using print 
Java :: java convert float to double 
Java :: has been compiled by a more recent version of the Java Runtime (class file version ), this version of the Java Runtime only recognizes class file versions up to 
Java :: hollow pyramid star pattern in java 
Java :: enum java 
Java :: center textview programmatically android 
Java :: Java loop throug gson JsonElement 
Java :: palindrome java 
Java :: java foreach backwards 
Java :: java enum 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =