Find the string formed by joining k consecutive nodes of linked list
Last Updated :
02 Jun, 2021
Given an integer K and a linked list in which each node stores a single character. The task is to join every K consecutive nodes of the linked list to form a single word. Finally, print the string obtained by joining these words (space separated).
Examples:
Input: List = ‘a’ -> ‘b’ -> ‘c’ ->’d’ -> ‘e’ -> NULL, k = 3
Output: abc de
The first three nodes form the first word “abc”
and next two nodes form the second word “de”.
Input: List = ‘a’ -> ‘b’ -> ‘c’ -> ‘d’ -> ‘e’ -> ‘f’ -> NULL, k = 2
Output: ab cd ef
Approach: The idea is to traverse the linked list and keep adding character present at each node to the word formed so far. Keep track of the number of nodes traversed and when the count becomes equal to k, add the word formed so far to the resultant string, reset word to an empty string and reset the count to zero. Repeat this until the entire linked list is not traversed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
Node* next;
};
Node* getNode( char data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
string findKWordString(Node* head, int k)
{
string ans = "" ;
int cnt = 0;
string word = "" ;
while (head) {
if (cnt == k) {
if (ans != "" ) {
ans = ans + " " ;
}
ans = ans + word;
word = "" ;
cnt = 0;
}
word = word + string(1, head->data);
cnt++;
head = head->next;
}
if (ans != " " ) {
ans = ans + " " ;
}
ans = ans + word;
return ans;
}
int main()
{
Node* head = getNode( 'a' );
head->next = getNode( 'b' );
head->next->next = getNode( 'c' );
head->next->next->next = getNode( 'd' );
head->next->next->next->next = getNode( 'e' );
int k = 3;
cout << findKWordString(head, k);
return 0;
}
|
Java
class GFG{
static class Node
{
char data;
Node next;
};
static Node getNode( char data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static String findKWordString(Node head, int k)
{
String ans = "" ;
int cnt = 0 ;
String word = "" ;
while (head != null )
{
if (cnt == k)
{
if (ans != "" )
{
ans = (ans + " " );
}
ans = ans + word;
word = "" ;
cnt = 0 ;
}
word = word + head.data;
cnt++;
head = head.next;
}
if (ans != " " )
{
ans = (ans + " " );
}
ans = ans + word;
return ans;
}
public static void main(String[] args)
{
Node head = getNode( 'a' );
head.next = getNode( 'b' );
head.next.next = getNode( 'c' );
head.next.next.next = getNode( 'd' );
head.next.next.next.next = getNode( 'e' );
int k = 3 ;
System.out.print(findKWordString(head, k));
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
def findKWordString(head,k):
ans = ""
cnt = 0
word = ""
while (head):
if (cnt = = k):
if (ans ! = ""):
ans = ans + " "
ans = ans + word
word = ""
cnt = 0
word = word + head.data
cnt + = 1
head = head. next
if (ans ! = " " ):
ans = ans + " "
ans = ans + word
return ans
if __name__ = = '__main__' :
head = Node( 'a' )
head. next = Node( 'b' )
head. next . next = Node( 'c' )
head. next . next . next = Node( 'd' )
head. next . next . next . next = Node( 'e' )
k = 3
print (findKWordString(head, k))
|
C#
using System;
class GFG{
class Node
{
public char data;
public Node next;
};
static Node getNode( char data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static String findKWordString(Node head, int k)
{
String ans = "" ;
int cnt = 0;
String word = "" ;
while (head != null )
{
if (cnt == k)
{
if (ans != "" )
{
ans = (ans + " " );
}
ans = ans + word;
word = "" ;
cnt = 0;
}
word = word + head.data;
cnt++;
head = head.next;
}
if (ans != " " )
{
ans = (ans + " " );
}
ans = ans + word;
return ans;
}
public static void Main(String[] args)
{
Node head = getNode( 'a' );
head.next = getNode( 'b' );
head.next.next = getNode( 'c' );
head.next.next.next = getNode( 'd' );
head.next.next.next.next = getNode( 'e' );
int k = 3;
Console.Write(findKWordString(head, k));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function getNode( data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function findKWordString(head , k) {
var ans = "" ;
var cnt = 0;
var word = "" ;
while (head != null ) {
if (cnt == k) {
if (ans != "" ) {
ans = (ans + " " );
}
ans = ans + word;
word = "" ;
cnt = 0;
}
word = word + head.data;
cnt++;
head = head.next;
}
if (ans != " " ) {
ans = (ans + " " );
}
ans = ans + word;
return ans;
}
var head = getNode( 'a' );
head.next = getNode( 'b' );
head.next.next = getNode( 'c' );
head.next.next.next = getNode( 'd' );
head.next.next.next.next = getNode( 'e' );
var k = 3;
document.write(findKWordString(head, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...