Implement LinkedList class in pythonCritique of LinkedList classOptimising LinkedList class - Part 2LinkedList (doubly) implementationPython class to implement a list with a size limitLinkedList implementation in PythonPython 2 Stack and LinkedList implementationLinkedList class implementation in PythonLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)Simple LinkedListA LinkedList implementation in Python
Opacity of an object in 2.8
How could a scammer know the apps on my phone / iTunes account?
Why doesn't the EU now just force the UK to choose between referendum and no-deal?
Why would a flight no longer considered airworthy be redirected like this?
How do I hide Chekhov's Gun?
Why Choose Less Effective Armour Types?
How can I track script which gives me "command not found" right after the login?
Recruiter wants very extensive technical details about all of my previous work
How to write cleanly even if my character uses expletive language?
Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?
Are all passive ability checks floors for active ability checks?
What is the significance behind "40 days" that often appears in the Bible?
Is there a data structure that only stores hash codes and not the actual objects?
Why does Bach not break the rules here?
Identifying the interval from A♭ to D♯
How to terminate ping <dest> &
How Could an Airship Be Repaired Mid-Flight
Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?
Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible
My adviser wants to be the first author
How to simplify this time periods definition interface?
How to explain that I do not want to visit a country due to personal safety concern?
Can a druid choose the size of its wild shape beast?
What did Alexander Pope mean by "Expletives their feeble Aid do join"?
Implement LinkedList class in python
Critique of LinkedList classOptimising LinkedList class - Part 2LinkedList (doubly) implementationPython class to implement a list with a size limitLinkedList implementation in PythonPython 2 Stack and LinkedList implementationLinkedList class implementation in PythonLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)Simple LinkedListA LinkedList implementation in Python
$begingroup$
I am currently study the basic data structure and trying to implement everything as I go. can anyone give me some feedback about class LinkedList how to make the code more elegant. any review are appreciated.
class Node(object):
def __init__(self, data, n=None):
self.val = data
self.next = n
def get(self):
return self.data
def set(self, data):
self.val = data
def get_next_node(self):
return self.next
def set_next_node(self, data):
self.next.val = data
class SingleLinkedList(object):
"""
Single Linked List object
Args:
data(Node): Node
Attributes:
head(Node): single LinkedList head
"""
def __init__(self, data):
self.head = data
def __repr__(self):
"""
:return:
"""
cur = self.head
s = ''
while cur:
s += f'cur.val->'
cur = cur.next
return s
def __len__(self):
cur, cnt = self.head, 0
while cur:
cnt += 1
cur = cur.next
return cnt
def append(self, data):
if not self.head:
self.head = Node(data)
return
cur = self.head
while cur.next: cur = cur.next
cur.next = Node(data)
def insert_before_key(self, key, data):
cur = self.head
prev = Node(None)
while cur:
if cur.val == key:
node = Node(data)
node.next = cur
prev.next = node
break
prev = cur
cur = cur.next
self.head = prev.next
def insert_after_key(self, key, data):
cur = self.head
while cur:
if cur.val == key:
node = Node(data)
node.next = cur.next
cur.next = node
cur = cur.next
def delete(self, key):
if not self.head: return
dummy = cur = self.head
prev = None
while cur:
if cur.val == key and prev:
prev.next = cur.next
self.head = dummy
return
elif cur.val == key:
self.head = cur.next
return
prev = cur
cur = cur.next
def search(self, key):
cur = self.head
while cur and cur.val != key:
cur = cur.next
return cur or None
def reverse(self):
cur = self.head
prev = None
while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
self.head = prev
```
python-3.x linked-list
$endgroup$
add a comment |
$begingroup$
I am currently study the basic data structure and trying to implement everything as I go. can anyone give me some feedback about class LinkedList how to make the code more elegant. any review are appreciated.
class Node(object):
def __init__(self, data, n=None):
self.val = data
self.next = n
def get(self):
return self.data
def set(self, data):
self.val = data
def get_next_node(self):
return self.next
def set_next_node(self, data):
self.next.val = data
class SingleLinkedList(object):
"""
Single Linked List object
Args:
data(Node): Node
Attributes:
head(Node): single LinkedList head
"""
def __init__(self, data):
self.head = data
def __repr__(self):
"""
:return:
"""
cur = self.head
s = ''
while cur:
s += f'cur.val->'
cur = cur.next
return s
def __len__(self):
cur, cnt = self.head, 0
while cur:
cnt += 1
cur = cur.next
return cnt
def append(self, data):
if not self.head:
self.head = Node(data)
return
cur = self.head
while cur.next: cur = cur.next
cur.next = Node(data)
def insert_before_key(self, key, data):
cur = self.head
prev = Node(None)
while cur:
if cur.val == key:
node = Node(data)
node.next = cur
prev.next = node
break
prev = cur
cur = cur.next
self.head = prev.next
def insert_after_key(self, key, data):
cur = self.head
while cur:
if cur.val == key:
node = Node(data)
node.next = cur.next
cur.next = node
cur = cur.next
def delete(self, key):
if not self.head: return
dummy = cur = self.head
prev = None
while cur:
if cur.val == key and prev:
prev.next = cur.next
self.head = dummy
return
elif cur.val == key:
self.head = cur.next
return
prev = cur
cur = cur.next
def search(self, key):
cur = self.head
while cur and cur.val != key:
cur = cur.next
return cur or None
def reverse(self):
cur = self.head
prev = None
while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
self.head = prev
```
python-3.x linked-list
$endgroup$
add a comment |
$begingroup$
I am currently study the basic data structure and trying to implement everything as I go. can anyone give me some feedback about class LinkedList how to make the code more elegant. any review are appreciated.
class Node(object):
def __init__(self, data, n=None):
self.val = data
self.next = n
def get(self):
return self.data
def set(self, data):
self.val = data
def get_next_node(self):
return self.next
def set_next_node(self, data):
self.next.val = data
class SingleLinkedList(object):
"""
Single Linked List object
Args:
data(Node): Node
Attributes:
head(Node): single LinkedList head
"""
def __init__(self, data):
self.head = data
def __repr__(self):
"""
:return:
"""
cur = self.head
s = ''
while cur:
s += f'cur.val->'
cur = cur.next
return s
def __len__(self):
cur, cnt = self.head, 0
while cur:
cnt += 1
cur = cur.next
return cnt
def append(self, data):
if not self.head:
self.head = Node(data)
return
cur = self.head
while cur.next: cur = cur.next
cur.next = Node(data)
def insert_before_key(self, key, data):
cur = self.head
prev = Node(None)
while cur:
if cur.val == key:
node = Node(data)
node.next = cur
prev.next = node
break
prev = cur
cur = cur.next
self.head = prev.next
def insert_after_key(self, key, data):
cur = self.head
while cur:
if cur.val == key:
node = Node(data)
node.next = cur.next
cur.next = node
cur = cur.next
def delete(self, key):
if not self.head: return
dummy = cur = self.head
prev = None
while cur:
if cur.val == key and prev:
prev.next = cur.next
self.head = dummy
return
elif cur.val == key:
self.head = cur.next
return
prev = cur
cur = cur.next
def search(self, key):
cur = self.head
while cur and cur.val != key:
cur = cur.next
return cur or None
def reverse(self):
cur = self.head
prev = None
while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
self.head = prev
```
python-3.x linked-list
$endgroup$
I am currently study the basic data structure and trying to implement everything as I go. can anyone give me some feedback about class LinkedList how to make the code more elegant. any review are appreciated.
class Node(object):
def __init__(self, data, n=None):
self.val = data
self.next = n
def get(self):
return self.data
def set(self, data):
self.val = data
def get_next_node(self):
return self.next
def set_next_node(self, data):
self.next.val = data
class SingleLinkedList(object):
"""
Single Linked List object
Args:
data(Node): Node
Attributes:
head(Node): single LinkedList head
"""
def __init__(self, data):
self.head = data
def __repr__(self):
"""
:return:
"""
cur = self.head
s = ''
while cur:
s += f'cur.val->'
cur = cur.next
return s
def __len__(self):
cur, cnt = self.head, 0
while cur:
cnt += 1
cur = cur.next
return cnt
def append(self, data):
if not self.head:
self.head = Node(data)
return
cur = self.head
while cur.next: cur = cur.next
cur.next = Node(data)
def insert_before_key(self, key, data):
cur = self.head
prev = Node(None)
while cur:
if cur.val == key:
node = Node(data)
node.next = cur
prev.next = node
break
prev = cur
cur = cur.next
self.head = prev.next
def insert_after_key(self, key, data):
cur = self.head
while cur:
if cur.val == key:
node = Node(data)
node.next = cur.next
cur.next = node
cur = cur.next
def delete(self, key):
if not self.head: return
dummy = cur = self.head
prev = None
while cur:
if cur.val == key and prev:
prev.next = cur.next
self.head = dummy
return
elif cur.val == key:
self.head = cur.next
return
prev = cur
cur = cur.next
def search(self, key):
cur = self.head
while cur and cur.val != key:
cur = cur.next
return cur or None
def reverse(self):
cur = self.head
prev = None
while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
self.head = prev
```
python-3.x linked-list
python-3.x linked-list
asked 7 mins ago
A.LeeA.Lee
493
493
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215546%2fimplement-linkedlist-class-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215546%2fimplement-linkedlist-class-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown