StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Wednesday, September 26, 2012

Check if a linked list is a palindrome: Non-recursive version

 September 26, 2012     Programming problems in CPP     No comments   

Write a program to check if a linked list is a palindrome without using recursion.

The approach:-

  1. Solution for this problem is to reach midway of the list and start comparing both sides as we move from midway to end of list.

  2. The first half of the list node pointers are pushed to stack (implemented as array here).


The solution:-

#include <iostream>

using namespace std;

// General list node
class Node {
public:
Node() { mData = -1; mNext = NULL; }
~Node() {}
int data() { return mData; }
void setData(int data) { mData = data; }
Node* next() { return mNext; }
void setNext(Node* next) { mNext = next; }
private:
int mData;
Node* mNext;
};

// General list class
class List {
public:
List() { mHead = NULL; mCount = 0; }
~List() {}
Node* head() { return mHead; }
void setHead(Node* head) { mHead = head; }

void append(int data) {
Node* tmp = new Node();
tmp->setData(data);
if ( mHead == NULL )
mHead = tmp;
else {
Node* ptr = mHead;
while ( ptr->next() != NULL ) {
ptr = ptr->next();
}
ptr->setNext(tmp);
}
}

void prepend(int data) {
Node* tmp = new Node();
tmp->setData(data);
tmp->setNext(mHead);
mHead = tmp;
}

void print() {
Node* ptr = mHead;
while ( ptr != NULL ) {
cout <data() <next();
}
cout <next();
}
return count;
}

private:
Node* mHead;
int mCount;
};

bool isListPalindrome(List* l)
{
int size = l->size();
int len = size/2;
Node **stackArray = new Node*[len];
Node* node = l->head();
int i = 0;
while(len-- > 0) {
stackArray[i++] = node;
node = node->next();
}
i--;
if (size & 0x1)
node = node->next();

while (node)
{
if (((Node *)stackArray[i])->data() != node->data()) {
delete [] stackArray;
return false;
}
node = node->next();
i--;
}

delete [] stackArray;
return true;
}

int main() {
List* l = new List();
l->append(100);
l->append(200);
l->append(300);
l->append(400);
l->append(300);
l->append(200);
l->append(100);
l->print();

bool stat = isListPalindrome(l);
if ( stat ) {
cout << "Linked list is palindrome" << endl;
}
else {
cout << "Linked list is not a palindrome" << endl;
}

delete l;
return 0;
}

Output:-
100 200 300 400 300 200 100
Linked list is palindrome
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk