Skip to content

Commit 9d00fec

Browse files
committed
Reverse Linked List solition added
1 parent c419eaa commit 9d00fec

File tree

18 files changed

+111
-17
lines changed

18 files changed

+111
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| Python | [Python](Python/README.md) | [@muffafa](https://github.com/muffafa) | 14 |
66
| Java | [JAVA](Java/README.md) | [@cagridemirtash](https://github.com/cagridemirtash) | 3 |
77
| Javascript | [Javascript](Javascript/README.md) | [@kaanncavdar](https://github.com/kaanncavdar) | 4 |
8-
| Rust | [Rust](Rust/README.md) | [@sektor7k](https://github.com/sektor7k) | 11 |
8+
| Rust | [Rust](Rust/README.md) | [@sektor7k](https://github.com/sektor7k) | 12 |
99
| C++ | [Cpp](Cpp/README.md) | [@sametaydinq](https://github.com/sametaydinq) | 0 |
1010

1111
---

Rust/0001-two-sum/two-sum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Brute Force to solve the Two-Sum Problem on Rust language
22

33

4-
## Complexity
4+
## 🧑🏻‍💻 Approach
55

66
The function `two_sum` takes two parameters: a vector of integers `nums` and a target integer `target`. It aims to find two numbers in the `nums` vector whose sum equals the `target`. The function returns a vector containing the indices of these two numbers.
77

Rust/0020-valid-parentheses/1-answer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [✅ Stack and Map to solve the 🧑🏻‍💻 Valid Parentheses on 🦀 RUST language]()
22

3-
## 🧩 Complexity
3+
## 🧑🏻‍💻 Approach
44

55
Create an empty vector called `result` to act as a stack.
66

Rust/0049-group-anagram/group-anagram.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ✅ Counts the Frequencies to solve the 🧑🏻‍💻 Group Anagram Problem on 😎 Rust language
22

33

4-
## Complexity
4+
## 🧑🏻‍💻 Approach
55

66
This code contains a function called `group_anagrams`, which takes a `Vec<String>` parameter named `strs` and returns a vector containing the groups of anagrams.
77

Rust/0121-best-time-to-buy-and-sell-stock/1-answer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [✅ Double Stack to solve the 🧑🏻‍💻 Best Time to Buy and Sell Stock Problem on 🦀 RUST language](#)
22

3-
## 🧩 Complexity
3+
## 🧑🏻‍💻 Approach
44

55
The function `max_profit` takes a vector of integers `prices` as input and returns an integer representing the maximum profit that can be obtained from buying and selling a stock.
66

Rust/0125-valid-palindrome/1-answer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [✅ Lowercase Alphanumeric to solve the 🧑🏻‍💻 Valid Palindrome Problem on 🦀 Python language]()
22

3-
## 🧩 Complexity
3+
## 🧑🏻‍💻 Approach
44

55
The `Solution` struct contains a public function `is_palindrome` that takes a `String` parameter `s` and returns a boolean value. The goal of the function is to determine if the given string `s` is a palindrome.
66

Rust/0155-min-stack/1-answer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [✅ Double Stack to solve the 🧑🏻‍💻 Min Stack Problem on 🦀 RUST language]()
22

3-
## 🧩 Complexity
3+
## 🧑🏻‍💻 Approach
44

55
`new()`: This is the constructor method that creates a new instance of `MinStack` by initializing an empty `Vec` using `Vec::new()`.
66

Rust/0155-min-stack/2-answer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [✅ Double Stack to solve the 🧑🏻‍💻 Min Stack Problem on 🦀 RUST language]()
22

3-
## 🧩 Complexity
3+
## 🧑🏻‍💻 Approach
44

55
`new()`: This is the constructor method that creates a new instance of `MinStack` by initializing empty vectors for `stack` and `min_stack` using `Vec::new()`.
66

Rust/0167-two-sum-2/1-answer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Brute Force to solve the Two-Sum 2 Problem on Rust language
22

3-
## Complexity
3+
## 🧑🏻‍💻 Approach
44

55
The function `two_sum` takes two parameters: a vector of integers `nums` and a target integer `target`. It aims to find two numbers in the `nums` vector whose sum equals the `target`. The function returns a vector containing the indices of these two numbers.
66

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [✅ Iteratively solving the 🧑🏻‍💻 Reverse Linked List Problem on 🦀 Rust language]()
2+
3+
## 🧑🏻‍💻 Approach
4+
5+
The given code defines a struct `ListNode` that represents a node of a linked list. It has two fields: `val`, which stores the value of the node, and `next`, which is an `Option` containing the next node in the list.
6+
7+
The `impl` block contains an implementation for the `ListNode` struct. It defines a constructor function `new` that creates a new `ListNode` with the given value and initializes `next` to `None`.
8+
9+
The `reverse_list` function takes an `Option<Box<ListNode>>` as input, representing the head of a linked list, and returns the head of the reversed linked list.
10+
11+
Inside the function, two variables `current` and `previous` are initialized. `current` holds the current node being processed, and `previous` holds the reversed portion of the linked list.
12+
13+
The function enters a loop, where it iteratively reverses the linked list. It uses a combination of `while let` and `Option::take()` to move the current node (`node`) out of the Option and assign its next to `current`.
14+
15+
Within each iteration, the `next` node is temporarily stored, and the `next` field of the current node is set to `previous`, effectively reversing the pointer direction.
16+
17+
Finally, the `previous` node becomes the new `current` node, and the `next` node becomes the new `current` for the next iteration.
18+
19+
Once the loop completes, the reversed linked list is stored in `previous`, which now represents the new head of the reversed list, and it is returned.
20+
21+
## 🔐 Code
22+
23+
``` rust
24+
// Definition for singly-linked list.
25+
// #[derive(PartialEq, Eq, Clone, Debug)]
26+
// pub struct ListNode {
27+
// pub val: i32,
28+
// pub next: Option<Box<ListNode>>
29+
// }
30+
//
31+
// impl ListNode {
32+
// #[inline]
33+
// fn new(val: i32) -> Self {
34+
// ListNode {
35+
// next: None,
36+
// val
37+
// }
38+
// }
39+
// }
40+
impl Solution {
41+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
42+
let mut current = head;
43+
let mut previous = None;
44+
45+
while let Some(mut node) = current{
46+
let next = node.next.take();
47+
node.next=previous;
48+
previous = Some(node);
49+
current = next;
50+
}
51+
52+
previous
53+
}
54+
}
55+
```

0 commit comments

Comments
 (0)