Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Rotate a linked list

/*
	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

rotate linked list

def rotate(self, k):
  if self.head and self.head.next:
    p = self.head 
    q = self.head 
    prev = None
    count = 0
    
    while p and count < k:
        prev = p
        p = p.next 
        q = q.next 
        count += 1
    p = prev
    while q:
        prev = q 
        q = q.next 
    q = prev 

    q.next = self.head 
    self.head = p.next 
    p.next = None
Comment

PREVIOUS NEXT
Code Example
Python :: size of int in python 
Python :: python generate tuple from lists 
Python :: format numbers in column to percentage in python 
Python :: pandas get attribute of object 
Python :: split list python percent 
Python :: pip in python 
Python :: requesting with aiohttp 
Python :: whitelist the ip address django 
Python :: Total processing python 
Python :: pydrive download by url 
Python :: subscriptable meaning in python 
Python :: cascade models in django 
Python :: Python Join Lists 
Python :: random seed python 
Python :: python check if false in dict 
Python :: python access class variable by string 
Python :: how to change an integer to a string in python permanently 
Python :: is python easy or hard to learn 
Python :: max python 
Python :: allow x_frame_options django 
Python :: detect gender from name 
Python :: how to run python file in when windows startup 
Python :: Write a Python program to remove a key from a dictionary. 
Python :: parse_dates 
Python :: python typing module list 
Python :: python booleans 
Python :: for in loop python 
Python :: length of dictionary in python 
Python :: install python package 
Python :: default python packages 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =