Skip to content

Commit 37f6a3d

Browse files
3 LinkedList codes
1 parent 49b35b9 commit 37f6a3d

File tree

6 files changed

+158
-53
lines changed

6 files changed

+158
-53
lines changed

.idea/AlgorithmsByPython.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 76 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Given a linked list, remove the nth node from the end of list and return its head.
3+
For example,
4+
Given linked list: 1->2->3->4->5, and n = 2.
5+
After removing the second node from the end, the linked list becomes 1->2->3->5.
6+
'''
7+
class ListNode(object):
8+
def __init__(self, x):
9+
self.val = x
10+
self.next = None
11+
12+
class Solution(object):
13+
def removeNthFromEnd(self, head, n):
14+
if not head and n <= 0:
15+
return None
16+
pNode = ListNode(0)
17+
pNode.next = head
18+
first, second = pNode, pNode
19+
for i in range(n):
20+
if first.next:
21+
first = first.next
22+
else:
23+
return None
24+
while first.next:
25+
first = first.next
26+
second = second.next
27+
second.next = second.next.next
28+
return pNode.next

leetcode/2. Add Two Numbers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
3+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
4+
Output: 7 -> 0 -> 8
5+
'''
6+
class ListNode(object):
7+
def __init__(self, x):
8+
self.val = x
9+
self.next = None
10+
11+
class Solution(object):
12+
def addTwoNumbers(self, l1, l2):
13+
pNode = ListNode(0)
14+
pHead = pNode
15+
val = 0
16+
while l1 or l2 or val:
17+
if l1:
18+
val += l1.val
19+
l1 = l1.next
20+
if l2:
21+
val += l2.val
22+
l2 = l2.next
23+
pNode.next = ListNode(val % 10)
24+
val /= 10
25+
pNode = pNode.next
26+
return pHead.next
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Remove all elements from a linked list of integers that have value val.
3+
Example
4+
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
5+
Return: 1 --> 2 --> 3 --> 4 --> 5
6+
'''
7+
# Definition for singly-linked list.
8+
class ListNode(object):
9+
def __init__(self, x):
10+
self.val = x
11+
self.next = None
12+
13+
class Solution(object):
14+
15+
def removeElements(self, head, val):
16+
preNode, res = None, head
17+
while head:
18+
if head.val == val:
19+
if not preNode:
20+
res = res.next
21+
else:
22+
preNode.next = head.next
23+
else:
24+
preNode = head
25+
head = head.next
26+
return res

0 commit comments

Comments
 (0)