Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. LinkedList

RearrangeLastN

Given a singly linked list of integers l and a non-negative integer n, move the last n list nodes to the beginning of the linked list.

Example

  • For l = [1, 2, 3, 4, 5] and n = 3, the output should be solution(l, n) = [3, 4, 5, 1, 2];

  • For l = [1, 2, 3, 4, 5, 6, 7] and n = 1, the output should be solution(l, n) = [7, 1, 2, 3, 4, 5, 6].

Idea

Corner case+Iteration+divide-->merge

Check if n is 0 or greater than length of list, if so, cannot rearrange

Get length of list and iterate first half from 0 to (length-n), store in head

Get second half from length-n to end, store as newHead

newHead.next = head

Code

ListNode<Integer> solution(ListNode<Integer> l, int n) {
    int count = 0;
    ListNode<Integer> curr = l;
    while(curr!=null){
        count++;
        curr = curr.next;
    }
    if(count<=n || n==0){
        return l;
    }
    ListNode<Integer> head = l;
    int currInd = 0;
    ListNode<Integer> next = null;
    while(l!=null && currInd<(count-n)){
        next = l.next;
        currInd++;
        if(currInd>=(count-n)){
            l.next = null;
        }
        else{
            l = next;
        }
    }
    ListNode<Integer> newHead = next;
    while(next!=null && next.next!=null){
        next = next.next;
    }
    
    next.next = head;
    return newHead;
    
}
PreviousReverseNodeInKGroupNextRemove Duplicates From Sorted List

Last updated 3 years ago