Question

In: Computer Science

C++ language or Python. Linked Lists You are given a linked list that contains N integers....

C++ language or Python.

Linked Lists

You are given a linked list that contains N integers. You are to perform the following reverse operation on the list:

  • Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}.

  • Reverse the selected subpart such as {8,2} and {16,12}.

  • The list should now be {1,8,2,9,16,12}.

Your node definition should consist of 2 elements: the integer value and a pointer to a node.

Sample Input

9 2 18 24 3 5 7 9 6 12

Sample Output:

24 18 2 3 5 7 9 12 6

Solutions

Expert Solution

C++

// Iterative C++ program to reverse

// a linked list

#include <iostream>

using namespace std;

/* Link list node */

struct Node {

    int data;

    struct Node* next;

    Node(int data)

    {

        this->data = data;

        next = NULL;

    }

};

struct LinkedList {

    Node* head;

    LinkedList()

    {

        head = NULL;

    }

    /* Function to reverse the linked list */

    void reverse()

    {

        // Initialize current, previous and

        // next pointers

        Node* current = head;

        Node *prev = NULL, *next = NULL;

        while (current != NULL) {

            // Store next

            next = current->next;

            // Reverse current node's pointer

            current->next = prev;

            // Move pointers one position ahead.

            prev = current;

            current = next;

        }

        head = prev;

    }

    /* Function to print linked list */

    void print()

    {

        struct Node* temp = head;

        while (temp != NULL) {

            cout << temp->data << " ";

            temp = temp->next;

        }

    }

    void push(int data)

    {

        Node* temp = new Node(data);

        temp->next = head;

        head = temp;

    }

};

/* Driver program to test above function*/

int main()

{

    /* Start with the empty list */

    LinkedList ll;

    ll.push(20);

    ll.push(4);

    ll.push(15);

    ll.push(85);

    cout << "Given linked list\n";

    ll.print();

    ll.reverse();

    cout << "\nReversed Linked list \n";

    ll.print();

    return 0;

}

Python

# Python program to reverse a linked list

# Time Complexity : O(n)

# Space Complexity : O(1)

# Node class

class Node:

    # Constructor to initialize the node object

    def __init__(self, data):

        self.data = data

        self.next = None

class LinkedList:

    # Function to initialize head

    def __init__(self):

        self.head = None

    # Function to reverse the linked list

    def reverse(self):

        prev = None

        current = self.head

        while(current is not None):

            next = current.next

            current.next = prev

            prev = current

            current = next

        self.head = prev

         

    # Function to insert a new node at the beginning

    def push(self, new_data):

        new_node = Node(new_data)

        new_node.next = self.head

        self.head = new_node

    # Utility function to print the linked LinkedList

    def printList(self):

        temp = self.head

        while(temp):

            print temp.data,

            temp = temp.next

# Driver program to test above functions

llist = LinkedList()

llist.push(20)

llist.push(4)

llist.push(15)

llist.push(85)

print "Given Linked List"

llist.printList()

llist.reverse()

print "\nReversed Linked List"

llist.printList()


Related Solutions

C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the...
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the code there is instructions on where to write the code. I do not know how to write Linked Lists. Has to be in the C-language, Any help is greatly appreciated   //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void delink(void); void memexit(void); void wfile(void); /********************************************************************* this is the structure to hold a agent...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT