reverse-nodes-in-k-group 1.0.0
Reverse Nodes in k-Group
Loading...
Searching...
No Matches
main.cpp File Reference
#include <bits/stdc++.h>
Include dependency graph for main.cpp:

Go to the source code of this file.

Data Structures

struct  ListNode
 Definition for singly-linked list. More...
 
class  Solution
 

Functions

void print_tree (ListNode *head)
 
void delete_tree (ListNode *head)
 
int main ()
 

Function Documentation

◆ delete_tree()

void delete_tree ( ListNode head)

Definition at line 95 of file main.cpp.

96{
97 stack<ListNode*> s;
98 ListNode* ptr = head;
99 while (ptr != nullptr)
100 {
101 s.push(ptr);
102 ptr = ptr->next;
103 }
104 while (!s.empty())
105 {
106 ptr = s.top();
107 s.pop();
108 delete ptr;
109 }
110}
Definition for singly-linked list.
Definition main.cpp:9
ListNode * next
Definition main.cpp:11

References ListNode::next.

Referenced by main().

◆ main()

int main ( )

Definition at line 112 of file main.cpp.

113{
114 ListNode* head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
115 print_tree(head);
116 head = Solution().reverseKGroup(head, 2);
117 print_tree(head);
118 delete_tree(head);
119 return 0;
120}
ListNode * reverseKGroup(ListNode *head, int k)
Definition main.cpp:20
void print_tree(ListNode *head)
Definition main.cpp:84
void delete_tree(ListNode *head)
Definition main.cpp:95

References delete_tree(), print_tree(), and Solution::reverseKGroup().

◆ print_tree()

void print_tree ( ListNode head)

Definition at line 84 of file main.cpp.

85{
86 ListNode* ptr = head;
87 while (ptr != nullptr)
88 {
89 cout << ptr->val << ' ';
90 ptr = ptr->next;
91 }
92 cout << '\n';
93}
int val
Definition main.cpp:10

References ListNode::next, and ListNode::val.

Referenced by main().