forked from YearOfProgramming/2017Challenges
-
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.
- Loading branch information
Steven Landau
committed
Jan 18, 2017
1 parent
908a86a
commit 7261c03
Showing
9 changed files
with
130 additions
and
14 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,5 @@ | ||
{ | ||
"python.linting.pylintEnabled": true, | ||
"python.pythonPath": "python3", | ||
"python.formatting.provider": "autopep8" | ||
} |
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
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
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,25 @@ | ||
Reversing a linked list | ||
====== | ||
Idea | ||
---- | ||
Everyone should be somewhat familiar with the concept of a linked list by now. Today, you're job is to create a linked list and reverse it. Your program needs to be able to take strings from standard input, which will be used as each nodes data attribute. The strings in standard input will be space separated. Nodes are to be created and connected for these strings one after another (newest node is appended to the end of the linked list). | ||
|
||
As for the "reverse" function. You need to implement a function to reverse the linked list. However, you may not touch the data on any of the nodes. The list must be reversed by manipulating the "next" attribute of each node. | ||
|
||
The program should run in O(N) time. | ||
|
||
You should have another function that will print the linked list (simply loop through it and print the data at each node). | ||
|
||
Testing | ||
------ | ||
In order to test the program, you may use the provided test files. Plug all strings from the test files into your program to create a node for each one. Print the list of nodes then reverse the list. After the list has been completely reversed output the list again (which should result in the reverse of what was previously printed). | ||
|
||
The test inputs are also listed below. | ||
|
||
1. r e d r u m | ||
|
||
2. p a r k | ||
|
||
3. b o o b y t r a p | ||
|
||
4. l i v e |
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,68 @@ | ||
/** | ||
* Created by slandau on 1/11/17. | ||
*/ | ||
public class Reverse<T> { | ||
public Node root = null; | ||
public int size = 0; | ||
|
||
private class Node { | ||
public T data; | ||
public Node next; | ||
|
||
public Node(T data) { | ||
this.data = data; | ||
} | ||
} | ||
|
||
public void append(T data) { | ||
if (size == 0) { | ||
root = new Node(data); | ||
} else { | ||
Node temp = root; | ||
while (temp.next != null) { | ||
temp = temp.next; | ||
} | ||
temp.next = new Node(data); | ||
} | ||
size++; | ||
} | ||
|
||
public void reverse() { | ||
if (size == 1) { | ||
// do nothing | ||
} else { | ||
Node prev = null; | ||
Node cur = root; | ||
Node next = root.next; | ||
while (next != null) { | ||
cur.next = prev; | ||
prev = cur; | ||
cur = next; | ||
next = next.next; | ||
} | ||
cur.next = prev; | ||
root = cur; | ||
} | ||
|
||
} | ||
|
||
public void print() { | ||
Node temp = root; | ||
while (temp != null) { | ||
System.out.println(temp.data); | ||
temp = temp.next; | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
Reverse<Integer> a = new Reverse<>(); | ||
a.append(1); | ||
a.append(2); | ||
a.append(3); | ||
a.print(); | ||
a.reverse(); | ||
a.print(); | ||
} | ||
|
||
} |
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 @@ | ||
r e d r u m |
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 @@ | ||
p a r k |
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 @@ | ||
b o o b y t r a p |
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 @@ | ||
l i v e |