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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

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+
```
18.7 KB
Loading
7.63 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
2+
3+
## 🚨 Problem
4+
<!-- Explanation of problem. -->
5+
Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
6+
7+
**Example 1:**
8+
<!-- An example of problem. -->
9+
![example 1](./img/rev1ex1.jpg)
10+
11+
>**Input:** head = \[1,2,3,4,5\] </br> <!-- Input example. -->
12+
**Output:** \[5,4,3,2,1\] </br> <!-- Output example. -->
13+
14+
**Example 2:**
15+
<!-- An example of problem. -->
16+
![example 2](./img/rev1ex2.jpg)
17+
18+
>**Input:** head = \[1,2\] </br> <!-- Input example. -->
19+
**Output:** \[2,1\] </br> <!-- Output example. -->
20+
21+
**Example 3:**
22+
<!-- An example of problem. -->
23+
24+
>**Input:** head = \[\] </br> <!-- Input example. -->
25+
**Output:** \[\] </br> <!-- Output example. -->
26+
27+
**Constraints:**
28+
<!-- Constraints of problem. -->
29+
- The number of nodes in the list is the range `[0, 5000]`.
30+
- `-5000 <= Node.val <= 5000`
31+
32+
**Follow-up:**
33+
<!-- Do more! -->
34+
A linked list can be reversed either iteratively or recursively. Could you implement both?
35+
36+
## πŸ” Solutions
37+
<!-- Solutions of problem and their links. -->
38+
39+
| ID | METHOD |
40+
| :-- | :----------------------: |
41+
| 1 | [Iterative](1-answer.md) |
42+

β€ŽRust/0217-contains-duplicate/1-answer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Contains Duplicate on Rust language
22

3-
## Complexity
3+
## πŸ§‘πŸ»β€πŸ’» Approach
44

55
First, we import the necessary library std::collections::HashSet to use the HashSet data structure.ses a constant amount of memory, regardless of the size of the input array. Next, we define a function called contains_duplicate which takes a Vec<i32> input parameter named nums and returns a bool value. Inside the function, we create a HashSet of type HashSet<i32> to keep track of numbers we have encountered so far. Then, we start a loop for each element in the nums vector. At each iteration, we check if the element is already present in the set. If it is, it means we have encountered a duplicate element, so we return true. If the element is not present in the set, we add it to the set so that we can check if the next element is a duplicate. Once the loop is completed, it means there are no duplicate elements, so we return false. This way, the contains_duplicate function checks for the presence of duplicate elements in the given number vector. You can call the function and test it with sample cases to ensure its correctness. In this example, since the number 1 appears twice in the nums vector, the output will be "true". You can create your own test cases to verify that the function works correctly.
66

β€ŽRust/0238-product-of-array-except-self/1-answer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [βœ… Prefix and Postfix to solve the πŸ§‘πŸ»β€πŸ’» Product of Array Except Self Problem on πŸ¦€ Rust language]()
22

3-
## 🧩 Complexity
3+
## πŸ§‘πŸ»β€πŸ’» Approach
44

55
First, we assign the length of the `nums` vector to the variable `n`.
66

β€ŽRust/0242-valid-anagram/valid-anagram.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# βœ… Counts the Frequencies to solve the πŸ§‘πŸ»β€πŸ’» Valid Anagram Problem on 😎 Rust language
22

3-
## Approach
4-
<!-- Describe your approach to solving the problem. -->
5-
In summary, the approach counts the frequencies of characters in both strings using dictionaries and checks if the character frequencies in s and t are the same. If the character frequencies match, the method returns `True`, indicating that t is an anagram of s. Otherwise, it returns `False`.
6-
7-
## Complexity
3+
## πŸ§‘πŸ»β€πŸ’» Approach
84

95
This code contains a function `is_anagram` nested inside a structure called `Solution`. The function checks whether the given two input strings are anagrams of each other.
106

β€ŽRust/0347-top-k-frequent-elements/1-answer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [βœ… Counts and Sort to solve the πŸ§‘πŸ»β€πŸ’» Top K Frequent Elements Problem on πŸ¦€ RUST language]()
22

3-
## 🧩 Complexity
3+
## πŸ§‘πŸ»β€πŸ’» Approach
44

55
You defined a helper function called `remove_duplicates`. This function removes duplicate numbers from the given vector and returns a sorted vector. It achieves this by first converting the input vector into a `HashSet`, which automatically filters out duplicates. Then, it converts the `HashSet` back into a `Vec` and sorts it to maintain the order of the numbers.
66

β€ŽRust/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| 9 | 0155 | [Min Stack](0155-min-stack/question.md) | Stack | Medium | Amazon |
1616
| 10 | 0167 | [Two Sum II Input Array Is Sorted](0167-two-sum-2/question.md) | Two Pointers | Medium | Amazon |
1717
| 11 | 0121 | [Best Time to Buy and Sell Stock](0121-best-time-to-buy-and-sell-stock/question.md) | Sliding Window | Easy | |
18+
| 12 | 0206 | [Reverse Linked List](0206-reverse-linked-list/question.md) | Linked List | Easy |
1819

1920
<div align="center">
2021
<h1>Awesome Leetcode Algorithms Solututions With Rust</h1>

0 commit comments

Comments
Β (0)