forked from xcv58/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solutions for Reverse-Linked-List with all 7 languages
- Loading branch information
Showing
7 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <stdlib.h> | ||
|
||
struct ListNode { | ||
int val; | ||
struct ListNode *next; | ||
}; | ||
|
||
struct ListNode* reverseList(struct ListNode* head) { | ||
struct ListNode* newHead = NULL; | ||
while (head) { | ||
struct ListNode* tmpNode = head->next; | ||
head->next = newHead; | ||
newHead = head; | ||
head = tmpNode; | ||
} | ||
return newHead; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <stdlib.h> | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode *next; | ||
ListNode(int x) : val(x), next(NULL) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
ListNode* reverseList(ListNode* head) { | ||
struct ListNode* newHead = NULL; | ||
while (head) { | ||
struct ListNode* tmpNode = head->next; | ||
head->next = newHead; | ||
newHead = head; | ||
head = tmpNode; | ||
} | ||
return newHead; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* public int val; | ||
* public ListNode next; | ||
* public ListNode(int x) { val = x; } | ||
* } | ||
*/ | ||
public class Solution { | ||
public ListNode ReverseList(ListNode head) { | ||
ListNode newHead = null; | ||
while (head != null) { | ||
ListNode tmpNode = head.next; | ||
head.next = newHead; | ||
newHead = head; | ||
head = tmpNode; | ||
} | ||
return newHead; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Solution { | ||
public class ListNode { | ||
int val; | ||
ListNode next; | ||
ListNode(int x) { val = x; } | ||
} | ||
|
||
public ListNode reverseList(ListNode head) { | ||
ListNode newHead = null; | ||
while (head != null) { | ||
ListNode tmpNode = head.next; | ||
head.next = newHead; | ||
newHead = head; | ||
head = tmpNode; | ||
} | ||
return newHead; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val) { | ||
* this.val = val; | ||
* this.next = null; | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var reverseList = function(head) { | ||
var newHead = null; | ||
while (head !== null) { | ||
var tmpNode = head.next; | ||
head.next = newHead; | ||
newHead = head; | ||
head = tmpNode; | ||
} | ||
return newHead; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution: | ||
# @param {ListNode} head | ||
# @return {ListNode} | ||
def reverseList(self, head): | ||
new_head = None | ||
while head is not None: | ||
tmp_node = head.next | ||
head.next = new_head | ||
new_head = head | ||
head = tmp_node | ||
return new_head |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode | ||
# attr_accessor :val, :next | ||
# def initialize(val) | ||
# @val = val | ||
# @next = nil | ||
# end | ||
# end | ||
|
||
# @param {ListNode} head | ||
# @return {ListNode} | ||
def reverse_list(head) | ||
newHead = nil | ||
while !head.nil? | ||
tmpNode = head.next | ||
head.next = newHead | ||
newHead = head | ||
head = tmpNode | ||
end | ||
return newHead | ||
end |