Skip to content

Commit

Permalink
add solutions for Reverse-Linked-List with all 7 languages
Browse files Browse the repository at this point in the history
  • Loading branch information
xcv58 committed May 5, 2015
1 parent 9b84102 commit ab5e73d
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Reverse-Linked-List/Solution.c
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;
}
21 changes: 21 additions & 0 deletions Reverse-Linked-List/Solution.cpp
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;
}
};
20 changes: 20 additions & 0 deletions Reverse-Linked-List/Solution.cs
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;
}
}
18 changes: 18 additions & 0 deletions Reverse-Linked-List/Solution.java
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;
}
}
21 changes: 21 additions & 0 deletions Reverse-Linked-List/Solution.js
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;
};
17 changes: 17 additions & 0 deletions Reverse-Linked-List/Solution.py
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
21 changes: 21 additions & 0 deletions Reverse-Linked-List/Solution.rb
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

0 comments on commit ab5e73d

Please sign in to comment.