Created
September 13, 2023 00:36
-
-
Save aniltv06/2bea147adc9d82630102e6da1b87ff88 to your computer and use it in GitHub Desktop.
Print in Reverse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Given a pointer to the head of a singly-linked list, print each value from the reversed list. If the given list is empty, do not print anything. | |
Example | |
refers to the linked list with values | |
Print the following: | |
3 | |
2 | |
1 | |
Function Description | |
Complete the reversePrint function in the editor below. | |
reversePrint has the following parameters: | |
SinglyLinkedListNode pointer head: a reference to the head of the list | |
Prints | |
The values of each node in the reversed list. | |
Input Format | |
The first line of input contains , the number of test cases. | |
The input of each test case is as follows: | |
The first line contains an integer , the number of elements in the list. | |
Each of the next n lines contains a data element for a list node. | |
*/ | |
class SinglyLinkedListNode { | |
var data: Int | |
var next: SinglyLinkedListNode? | |
init(data: Int) { | |
self.data = data | |
} | |
} | |
func reversePrint(head: SinglyLinkedListNode?) { | |
if head == nil { | |
return | |
} | |
reversePrint(head: head?.next) | |
if let data = head?.data { | |
print(data) | |
} | |
} | |
/* | |
// Define an asynchronous version of reversePrint | |
func reversePrintAsync(head: SinglyLinkedListNode?) async { | |
if head == nil { | |
return | |
} | |
await reversePrintAsync(head: head?.next) | |
if let data = head?.data { | |
print(data) | |
} | |
} | |
*/ | |
// Create sample singly-linked list nodes with different data values | |
let node1 = SinglyLinkedListNode(data: 42) | |
let node2 = SinglyLinkedListNode(data: 15) | |
let node3 = SinglyLinkedListNode(data: 7) | |
// Link the nodes to form a linked list: 42 -> 15 -> 7 -> nil | |
node1.next = node2 | |
node2.next = node3 | |
/* | |
2 # Number of test cases | |
3 # Test Case 1: Number of elements in the list | |
1 | |
2 | |
3 | |
2 # Test Case 2: Number of elements in the list | |
4 | |
5 | |
*/ | |
/* | |
Certainly, let's dive into a detailed explanation of how the `reversePrint` function works: | |
1. **Function Signature:** | |
```swift | |
func reversePrint(head: SinglyLinkedListNode?) | |
``` | |
This function takes a single argument `head`, which is a reference to the head of a singly-linked list. It returns `Void`, meaning it doesn't return any value but instead prints the data values in reverse order. | |
2. **Base Case:** | |
```swift | |
if head == nil { | |
return | |
} | |
``` | |
The function starts by checking if the `head` is `nil`, which means the end of the list has been reached. If it's `nil`, it returns and exits the recursion. | |
3. **Recursive Call:** | |
```swift | |
reversePrint(head: head?.next) | |
``` | |
If the `head` is not `nil`, it proceeds to make a recursive call to `reversePrint` with the `next` node (`head?.next`). This effectively moves to the next node in the list. | |
4. **Printing the Data Value:** | |
```swift | |
if let data = head?.data { | |
print(data) | |
} | |
``` | |
After the recursion has reached the end of the list (the last node), it prints the data value of the current node (`head?.data`). This happens as the recursion unwinds, and each node's data value is printed in reverse order. | |
5. **Recursion Continues:** | |
The recursion continues until it reaches the base case (when `head` is `nil`). At that point, it starts returning from the recursive calls, and as it returns, it prints the data values in reverse order. | |
6. **Example Usage:** | |
Suppose you have a linked list with data values `1 -> 2 -> 3 -> nil`. When you call `reversePrint(head: head)`, here's what happens step by step: | |
- The initial call has `head` pointing to the first node with data `1`. | |
- It enters the recursion and moves to the next node (`2`). | |
- The recursion continues, moving to the next node (`3`). | |
- At this point, the base case is reached since `head` is `nil`. | |
- The function starts returning from the recursive calls in reverse order: `3`, `2`, `1`. | |
- Each data value is printed as it returns, resulting in the output: | |
``` | |
3 | |
2 | |
1 | |
``` | |
So, the `reversePrint` function uses recursion to traverse the linked list in reverse order, and as it unwinds the recursion, it prints the data values in reverse sequence. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment