-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathProgram8.cpp
More file actions
117 lines (99 loc) · 2.08 KB
/
Program8.cpp
File metadata and controls
117 lines (99 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
using namespace std;
// C# program to delete every k-th Node
// of a singly linked list.
using System;
class GFG
{
/* Linked list Node */
public class Node
{
public int data;
public Node next;
}
// To remove complete list (Needed for
// case when k is 1)
static Node freeList(Node node)
{
while (node != null)
{
Node next = node.next;
node = next;
}
return node;
}
// Deletes every k-th node and
// returns head of modified list.
static Node deleteKthNode(Node head, int k)
{
// If linked list is empty
if (head == null)
return null;
if (k == 1)
{
head = freeList(head);
return null;
}
// Initialize ptr and prev before
// starting traversal.
Node ptr = head, prev = null;
// Traverse list and delete
// every k-th node
int count = 0;
while (ptr != null)
{
// increment Node count
count++;
// check if count is equal to k
// if yes, then delete current Node
if (k == count)
{
// put the next of current Node in
// the next of previous Node
prev.next = ptr.next;
// set count = 0 to reach further
// k-th Node
count = 0;
}
// update prev if count is not 0
if (count != 0)
prev = ptr;
ptr = prev.next;
}
return head;
}
/* Function to print linked list */
static void displayList(Node head)
{
Node temp = head;
while (temp != null)
{
Console.Write(temp.data + " ");
temp = temp.next;
}
}
// Utility function to create a new node.
static Node newNode(int x)
{
Node temp = new Node();
temp.data = x;
temp.next = null;
return temp;
}
// Driver Code
public static void Main(String []args)
{
/* Start with the empty list */
Node head = newNode(1);
head.next = newNode(2);
head.next.next = newNode(3);
head.next.next.next = newNode(4);
head.next.next.next.next = newNode(5);
head.next.next.next.next.next = newNode(6);
head.next.next.next.next.next.next = newNode(7);
head.next.next.next.next.next.next.next = newNode(8);
int k = 3;
head = deleteKthNode(head, k);
displayList(head);
}
}