Skip to content

Commit 3e6d3f3

Browse files
committed
Reverse LinkedList and Leetcode 206
1 parent 9fe34d9 commit 3e6d3f3

File tree

7 files changed

+306
-0
lines changed

7 files changed

+306
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
** Reverse a singly linked list.
3+
**
4+
** Example:
5+
**
6+
** Input: 1->2->3->4->5->NULL
7+
** Output: 5->4->3->2->1->NULL
8+
**/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* struct ListNode {
13+
* int val;
14+
* ListNode *next;
15+
* ListNode(int x) : val(x), next(NULL) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
ListNode* reverseList(ListNode* head) {
21+
if (head == NULL)
22+
return head;
23+
24+
ListNode* prev = NULL;
25+
ListNode* current = head;
26+
ListNode* next = NULL;
27+
28+
while(current != NULL)
29+
{
30+
next = current->next;
31+
current->next = prev;
32+
prev = current;
33+
current = next;
34+
}
35+
36+
head = prev;
37+
return head;
38+
}
39+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
** Reverse a singly linked list.
3+
**
4+
** Example:
5+
**
6+
** Input: 1->2->3->4->5->NULL
7+
** Output: 5->4->3->2->1->NULL
8+
**/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* public class ListNode {
13+
* int val;
14+
* ListNode next;
15+
* ListNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
public ListNode reverseList(ListNode head) {
20+
if (head == null)
21+
{
22+
return head;
23+
}
24+
25+
ListNode prev = null;
26+
ListNode current = head;
27+
ListNode next = null;
28+
29+
while (current != null)
30+
{
31+
next = current.next;
32+
current.next = prev;
33+
prev = current;
34+
current = next;
35+
}
36+
37+
head = prev;
38+
return head;
39+
}
40+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Node
5+
{
6+
public:
7+
int data;
8+
Node* nextElement;
9+
10+
Node()
11+
{
12+
nextElement = nullptr;
13+
}
14+
};
15+
16+
class LinkedList
17+
{
18+
private:
19+
Node* head;
20+
21+
public:
22+
LinkedList()
23+
{
24+
head = nullptr;
25+
}
26+
27+
bool isEmpty()
28+
{
29+
if (head == nullptr)
30+
return true;
31+
else
32+
{
33+
return false;
34+
}
35+
}
36+
37+
// Print the LinkedList
38+
bool printList()
39+
{
40+
if (isEmpty())
41+
{
42+
cout << "List is Empty" << endl;
43+
return false;
44+
}
45+
46+
Node* temp = head;
47+
cout << "List: ";
48+
49+
while (temp != nullptr)
50+
{
51+
cout << temp->data << "->" ;
52+
temp = temp->nextElement;
53+
}
54+
cout << "null" << endl;
55+
return true;
56+
}
57+
58+
// insert a new node as the first element of the list
59+
void insertAtHead(int value)
60+
{
61+
Node* newNode = new Node();
62+
newNode->data = value;
63+
newNode->nextElement = head;
64+
65+
head = newNode;
66+
cout << value << " Inserted." << endl;
67+
}
68+
69+
void insertAtTail(int value)
70+
{
71+
Node* newNode = new Node();
72+
newNode->data = value;
73+
74+
if (isEmpty())
75+
{
76+
head = newNode;
77+
cout << "Inserted. " << endl;
78+
return;
79+
}
80+
81+
Node* temp = head;
82+
while(temp->nextElement != nullptr)
83+
{
84+
temp = temp->nextElement;
85+
}
86+
temp->nextElement = newNode;
87+
cout << "Inserted." << endl;
88+
}
89+
90+
void revereLinkedList()
91+
{
92+
if (head == NULL)
93+
{
94+
cout << "Empty Linked List. Cannot reverse." << endl;
95+
}
96+
97+
Node* prev = NULL;
98+
Node* current = head;
99+
Node* next = NULL;
100+
101+
while(current != NULL)
102+
{
103+
next = current->nextElement;
104+
current->nextElement = prev;
105+
prev = current;
106+
current = next;
107+
}
108+
head = prev;
109+
}
110+
};
111+
112+
int main()
113+
{
114+
LinkedList list; // LinkedList created
115+
116+
for (int i = 0; i < 10; i++)
117+
{
118+
list.insertAtHead(i+1);
119+
//list.printList();
120+
}
121+
122+
list.printList();
123+
124+
cout << "Reverse LinkedList: " << endl;
125+
list.revereLinkedList();
126+
list.printList();
127+
128+
return 0;
129+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
class Node {
2+
3+
int value;
4+
Node next;
5+
6+
Node(int d)
7+
{
8+
value = d;
9+
next = null;
10+
}
11+
}
12+
public class ReverseALinkedList
13+
{
14+
private Node head;
15+
16+
public boolean isEmpty()
17+
{
18+
if (head == null)
19+
return true;
20+
else
21+
return false;
22+
}
23+
24+
public void printList()
25+
{
26+
if (isEmpty())
27+
System.out.println("LinkedList is Empty");
28+
29+
Node temp = head;
30+
31+
while (temp != null)
32+
{
33+
System.out.print(temp.value + "->");
34+
temp = temp.next;
35+
}
36+
System.out.println("null");
37+
}
38+
39+
public void insertAtTail(int value)
40+
{
41+
Node newNode = new Node(value);
42+
43+
if (isEmpty())
44+
{
45+
newNode.next = head;
46+
head = newNode;
47+
System.out.println(value + " has been inserted.");
48+
return;
49+
}
50+
51+
Node temp = head;
52+
while (temp.next != null)
53+
{
54+
temp = temp.next;
55+
}
56+
temp.next = newNode;
57+
System.out.println(value + " has been inserted.");
58+
}
59+
60+
public void reverseLinkedList()
61+
{
62+
if (head == null)
63+
{
64+
System.out.println("Empty List. Cannot reverse!!");
65+
return;
66+
}
67+
68+
Node prev = null;
69+
Node current = head;
70+
Node next = null;
71+
72+
while(current != null)
73+
{
74+
next = current.next;
75+
current.next = prev;
76+
prev = current;
77+
current = next;
78+
}
79+
head = prev;
80+
}
81+
82+
public static void main(String[] args) {
83+
ReverseALinkedList list = new ReverseALinkedList();
84+
85+
for(int i = 0; i < 10; i++)
86+
{
87+
list.insertAtTail(i);
88+
}
89+
90+
list.printList();
91+
92+
list.reverseLinkedList();
93+
94+
System.out.println("Reversed List: ");
95+
list.printList();
96+
97+
}
98+
}

0 commit comments

Comments
 (0)