Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit37f6a3d

Browse files
3 LinkedList codes
1 parent49b35b9 commit37f6a3d

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 aboutcustomizing 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 aboutcustomizing 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 aboutcustomizing 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+
classListNode(object):
8+
def__init__(self,x):
9+
self.val=x
10+
self.next=None
11+
12+
classSolution(object):
13+
defremoveNthFromEnd(self,head,n):
14+
ifnotheadandn<=0:
15+
returnNone
16+
pNode=ListNode(0)
17+
pNode.next=head
18+
first,second=pNode,pNode
19+
foriinrange(n):
20+
iffirst.next:
21+
first=first.next
22+
else:
23+
returnNone
24+
whilefirst.next:
25+
first=first.next
26+
second=second.next
27+
second.next=second.next.next
28+
returnpNode.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+
classListNode(object):
7+
def__init__(self,x):
8+
self.val=x
9+
self.next=None
10+
11+
classSolution(object):
12+
defaddTwoNumbers(self,l1,l2):
13+
pNode=ListNode(0)
14+
pHead=pNode
15+
val=0
16+
whilel1orl2orval:
17+
ifl1:
18+
val+=l1.val
19+
l1=l1.next
20+
ifl2:
21+
val+=l2.val
22+
l2=l2.next
23+
pNode.next=ListNode(val%10)
24+
val/=10
25+
pNode=pNode.next
26+
returnpHead.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+
classListNode(object):
9+
def__init__(self,x):
10+
self.val=x
11+
self.next=None
12+
13+
classSolution(object):
14+
15+
defremoveElements(self,head,val):
16+
preNode,res=None,head
17+
whilehead:
18+
ifhead.val==val:
19+
ifnotpreNode:
20+
res=res.next
21+
else:
22+
preNode.next=head.next
23+
else:
24+
preNode=head
25+
head=head.next
26+
returnres

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp