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...
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...
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
C++ language. Suppose you are given a list of students registered in a course and you...
C++ language. Suppose you are given a list of students registered in a course and you are required to implement an Linked List of student. Each student in the list has name, regNo and cGpa. You task is to implement following function related to Linked List. insertStudent() : This function, when called will prompt user to enter his/her choice of insertion. The options are 1. Insert student at the start of linked list 2. Insert student at the end of...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
C programing language A file "data.txt" contains only integers. Write a code to find average of...
C programing language A file "data.txt" contains only integers. Write a code to find average of all values and print the average How would you use execlp function to execute "ps –e –a –l" command char *dt = "The five boxing wizards jump quickly"; write a program to count frequency of each letter, ignore case. Print the letter and frequency of each letter. // 1A: . Ask the user to enter a password string, store it in pass. Password should...
Python linked lists ● Insert: this method takes a value as a parameter, and adds a...
Python linked lists ● Insert: this method takes a value as a parameter, and adds a node which contains the value to the end of the linked list ● Delete: this method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list ● Find: this method takes a value as a parameter,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT