Monday, December 12, 2011

Reverse Linked List


2.Reverse single Linked List Recursive:

3.Reverse Doubly Linked List Iterative

4.Reverse Doubly Linked List Recursive
struct node* REVERSE_RECURSIVE_LIST_DOUBLE(struct node* head)
{   struct node* current=head;

    if(head == NULL || head->link == NULL)
        return head;
  
  
    struct node* head_ret = REVERSE_RECURSIVE_LIST_DOUBLE(current->link);
    struct node* next = current->link;

    next->link = current;
    current->link = current->prev;
    current->prev = next;

    return head_ret;

5. Reverse Linked List Variations:Question 0: Reverse a linked list from position m to n
http://www.programcreek.com/2014/06/leetcode-reverse-linked-list-ii-java/

6. Reverse a Linked List in groups of given size k


7. Reverse alternate K nodes in a Singly Linked List

8. Reverse k nodes from end
Given a linked list, write a function to reverse every k nodes
(where k is an input to the function) from the end of the linked list
in an efficient way. Give the complexity of your algorithm.
Example: Inputs: 1->2->3->4->5->6->7->8->9->10->NULL and k = 3
Output:1->4->3->2->7->6->5->10->9->8->NULL
Reverse a Linked List in size of 2
Given:1->2->3->4->5->6->7->8
Output:7->8->5->6->3->4->1->2

1 comment:

  1. //Complete Program for Reverse in groups of size k

    #include
    #include

    struct node
    {
    int data;
    struct node* next;
    };

    /* Reverses the linked list in groups of size k and returns the pointer to the new head node */
    struct node *reverse (struct node *head, int k)
    {
    struct node* current = head;
    struct node* next;
    struct node* prev = NULL;
    int count = 0;

    /*reverse first k nodes of the linked list */
    while (current != NULL && count < k)
    {
    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 new head of the input list */
    return prev;
    }

    /* UTILITY FUNCTIONS */
    /* Function to push a node */
    void push(struct node** head_ref, int new_data)
    {
    struct node* new_node =
    (struct node*) malloc(sizeof(struct node));

    new_node->data = new_data;

    new_node->next = (*head_ref);

    (*head_ref) = new_node;
    }

    void printList(struct node *node)
    {
    while(node != NULL)
    {
    printf("%d ", node->data);
    node = node->next;
    }
    }

    int main(void)
    {

    struct node* head = NULL;

    /* Created Linked list is 1->2->3->4->5->6->7->8 */
    push(&head, 8);
    push(&head, 7);
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);

    printf("\n Given linked list \n");
    printList(head);
    head = reverse(head, 3);

    printf("\n Reversed Linked list \n");
    printList(head);

    getchar();
    return(0);
    }

    ReplyDelete