mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge pull request #292 from morningsky/master
添加了 面试题02.07.链表相交 0019.删除链表的倒数第N个节点 0206.翻转链表 0206.翻转链表 python3版本
This commit is contained in:
@ -112,6 +112,30 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
|
||||
head_dummy = ListNode()
|
||||
head_dummy.next = head
|
||||
|
||||
slow, fast = head_dummy, head_dummy
|
||||
while(n!=0): #fast先往前走n步
|
||||
fast = fast.next
|
||||
n -= 1
|
||||
while(fast.next!=None):
|
||||
slow = slow.next
|
||||
fast = fast.next
|
||||
#fast 走到结尾后,slow的下一个节点为倒数第N个节点
|
||||
slow.next = slow.next.next #删除
|
||||
return head_dummy.next
|
||||
```
|
||||
Go:
|
||||
```Go
|
||||
/**
|
||||
|
@ -208,7 +208,23 @@ public ListNode removeElements(ListNode head, int val) {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def removeElements(self, head: ListNode, val: int) -> ListNode:
|
||||
dummy_head = ListNode(next=head) #添加一个虚拟节点
|
||||
cur = dummy_head
|
||||
while(cur.next!=None):
|
||||
if(cur.next.val == val):
|
||||
cur.next = cur.next.next #删除cur.next节点
|
||||
else:
|
||||
cur = cur.next
|
||||
return dummy_head.next
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -143,7 +143,25 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
#双指针
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def reverseList(self, head: ListNode) -> ListNode:
|
||||
cur = head
|
||||
pre = None
|
||||
while(cur!=None):
|
||||
temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next
|
||||
cur.next = pre #反转
|
||||
#更新pre、cur指针
|
||||
pre = cur
|
||||
cur = temp
|
||||
return pre
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -151,7 +151,44 @@ public class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.next = None
|
||||
|
||||
class Solution:
|
||||
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
|
||||
lengthA,lengthB = 0,0
|
||||
curA,curB = headA,headB
|
||||
while(curA!=None): #求链表A的长度
|
||||
curA = curA.next
|
||||
lengthA +=1
|
||||
|
||||
while(curB!=None): #求链表B的长度
|
||||
curB = curB.next
|
||||
lengthB +=1
|
||||
|
||||
curA, curB = headA, headB
|
||||
|
||||
if lengthB>lengthA: #让curA为最长链表的头,lenA为其长度
|
||||
lengthA, lengthB = lengthB, lengthA
|
||||
curA, curB = curB, curA
|
||||
|
||||
gap = lengthA - lengthB #求长度差
|
||||
while(gap!=0):
|
||||
curA = curA.next #让curA和curB在同一起点上
|
||||
gap -= 1
|
||||
|
||||
while(curA!=None):
|
||||
if curA == curB:
|
||||
return curA
|
||||
else:
|
||||
curA = curA.next
|
||||
curB = curB.next
|
||||
return None
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
Reference in New Issue
Block a user