Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@anil477
Created May 31, 2017 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/483c152c653acf28cae5b04e177b39d0 to your computer and use it in GitHub Desktop.
Save anil477/483c152c653acf28cae5b04e177b39d0 to your computer and use it in GitHub Desktop.
Reverse a Linked List in groups of given size and Reverse alternate K nodes in a Singly Linked List
// Reverse a Linked List in groups of given size - http://www.geeksforgeeks.org/?p=8014
// Reverse alternate K nodes in a Singly Linked List - http://www.geeksforgeeks.org/reverse-alternate-k-nodes-in-a-singly-linked-list/
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
/* Function to print linked list */
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
/* Drier program to test above functions */
public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push(8);
llist.push(7);
llist.push(6);
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
llist.push(1);
System.out.println("Given Linked List");
llist.printList();
llist.head = llist.reverse(llist.head, 2);
// llist.head = llist.reverseAfter(llist.head, 2);
llist.printList();
}
Node reverseAfter(Node node, int k)
{
Node current = node;
Node next = null, prev = null;
int count = 0;
while (current != null && count < k) {
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}
/* 2) Now head points to the kth node. So change next
of head to (k+1)th node*/
if (node != null) {
node.next = current;
}
/* 3) We do not want to reverse next k nodes. So move the current
pointer to skip next k nodes */
count = 0;
while (count < k - 1 && current != null) {
current = current.next;
count++;
}
/* 4) Recursively call for the list starting from current->next.
And make rest of the list as next of first node */
if (current != null) {
current.next = reverseAfter(current.next, k);
}
/* 5) prev is new head of the input list */
return prev;
}
Node reverse(Node head, int k)
{
Node current = head;
Node next = null;
Node prev = null;
int count = 0;
/* Reverse first k nodes of linked list */
while (count < k && current != null)
{
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}
/* next is now a pointer to (k+1)th node
Recursively call for the list starting from current.
And make rest of the list as next of first node */
if (next != null) {
head.next = reverse(next, k);
}
// prev is now head of input list
return prev;
}
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment