Skip to content

Commit

Permalink
Merge pull request #1775 from juguagua/leetcode-modify-the-code-of-th…
Browse files Browse the repository at this point in the history
…e-linked-list

更新链表部分: 0707.设计链表 python代码,java注释 和 面试题02.07.链表相交 python代码 js注释
  • Loading branch information
youngyangyang04 authored Nov 24, 2022
2 parents 14d0ddc + d9950ce commit feb3f9c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 75 deletions.
116 changes: 65 additions & 51 deletions problems/0707.设计链表.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class MyLinkedList {
head = new ListNode(0);
}

//获取第index个节点的数值
//获取第index个节点的数值,注意index是从0开始的,第0个节点就是头结点
public int get(int index) {
//如果index非法,返回-1
if (index < 0 || index >= size) {
Expand All @@ -319,12 +319,12 @@ class MyLinkedList {
return currentNode.val;
}

//在链表最前面插入一个节点
//在链表最前面插入一个节点,等价于在第0个元素前添加
public void addAtHead(int val) {
addAtIndex(0, val);
}

//在链表的最后插入一个节点
//在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
public void addAtTail(int val) {
addAtIndex(size, val);
}
Expand Down Expand Up @@ -481,76 +481,90 @@ class MyLinkedList {
Python:
```python
# 单链表
class Node:

def __init__(self, val):
self.val = val
class Node(object):
def __init__(self, x=0):
self.val = x
self.next = None


class MyLinkedList:
class MyLinkedList(object):

def __init__(self):
self._head = Node(0) # 虚拟头部节点
self._count = 0 # 添加的节点数
self.head = Node()
self.size = 0 # 设置一个链表长度的属性,便于后续操作,注意每次增和删的时候都要更新

def get(self, index: int) -> int:
def get(self, index):
"""
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
:type index: int
:rtype: int
"""
if 0 <= index < self._count:
node = self._head
for _ in range(index + 1):
node = node.next
return node.val
else:
if index < 0 or index >= self.size:
return -1
cur = self.head.next
while(index):
cur = cur.next
index -= 1
return cur.val

def addAtHead(self, val: int) -> None:
def addAtHead(self, val):
"""
Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
:type val: int
:rtype: None
"""
self.addAtIndex(0, val)
new_node = Node(val)
new_node.next = self.head.next
self.head.next = new_node
self.size += 1

def addAtTail(self, val: int) -> None:
def addAtTail(self, val):
"""
Append a node of value val to the last element of the linked list.
:type val: int
:rtype: None
"""
self.addAtIndex(self._count, val)
new_node = Node(val)
cur = self.head
while(cur.next):
cur = cur.next
cur.next = new_node
self.size += 1

def addAtIndex(self, index: int, val: int) -> None:
def addAtIndex(self, index, val):
"""
Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
:type index: int
:type val: int
:rtype: None
"""
if index < 0:
index = 0
elif index > self._count:
self.addAtHead(val)
return
elif index == self.size:
self.addAtTail(val)
return
elif index > self.size:
return

# 计数累加
self._count += 1

add_node = Node(val)
prev_node, current_node = None, self._head
for _ in range(index + 1):
prev_node, current_node = current_node, current_node.next
else:
prev_node.next, add_node.next = add_node, current_node

def deleteAtIndex(self, index: int) -> None:
node = Node(val)
pre = self.head
while(index):
pre = pre.next
index -= 1
node.next = pre.next
pre.next = node
self.size += 1

def deleteAtIndex(self, index):
"""
Delete the index-th node in the linked list, if the index is valid.
:type index: int
:rtype: None
"""
if 0 <= index < self._count:
# 计数-1
self._count -= 1
prev_node, current_node = None, self._head
for _ in range(index + 1):
prev_node, current_node = current_node, current_node.next
else:
prev_node.next, current_node.next = current_node.next, None


if index < 0 or index >= self.size:
return
pre = self.head
while(index):
pre = pre.next
index -= 1
pre.next = pre.next.next
self.size -= 1

# 双链表
# 相对于单链表, Node新增了prev属性
class Node:
Expand Down
55 changes: 31 additions & 24 deletions problems/面试题02.07.链表相交.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,28 @@ public class Solution {

class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
"""
根据快慢法则,走的快的一定会追上走得慢的。
在这道题里,有的链表短,他走完了就去走另一条链表,我们可以理解为走的快的指针。
那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个
位置相遇
"""
if headA is None or headB is None:
return None
cur_a, cur_b = headA, headB # 用两个指针代替a和b


while cur_a != cur_b:
cur_a = cur_a.next if cur_a else headB # 如果a走完了,那么就切换到b走
cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a

return cur_a
lenA, lenB = 0, 0
cur = headA
while cur: # 求链表A的长度
cur = cur.next
lenA += 1
cur = headB
while cur: # 求链表B的长度
cur = cur.next
lenB += 1
curA, curB = headA, headB
if lenA > lenB: # 让curB为最长链表的头,lenB为其长度
curA, curB = curB, curA
lenA, lenB = lenB, lenA
for _ in range(lenB - lenA): # 让curA和curB在同一起点上(末尾位置对齐)
curB = curB.next
while curA: # 遍历curA 和 curB,遇到相同则直接返回
if curA == curB:
return curA
else:
curA = curA.next
curB = curB.next
return None
```

### Go
Expand Down Expand Up @@ -248,19 +253,21 @@ var getListLen = function(head) {
}
var getIntersectionNode = function(headA, headB) {
let curA = headA,curB = headB,
lenA = getListLen(headA),
lenB = getListLen(headB);
if(lenA < lenB) {
// 下面交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时
lenA = getListLen(headA), // 求链表A的长度
lenB = getListLen(headB);
if(lenA < lenB) { // 让curA为最长链表的头,lenA为其长度

// 交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时
// 如果不加分号,下面两条代码等同于一条代码: [curA, curB] = [lenB, lenA]

[curA, curB] = [curB, curA];
[lenA, lenB] = [lenB, lenA];
}
let i = lenA - lenB;
while(i-- > 0) {
let i = lenA - lenB; // 求长度差
while(i-- > 0) { // 让curA和curB在同一起点上(末尾位置对齐)
curA = curA.next;
}
while(curA && curA !== curB) {
while(curA && curA !== curB) { // 遍历curA 和 curB,遇到相同则直接返回
curA = curA.next;
curB = curB.next;
}
Expand Down

0 comments on commit feb3f9c

Please sign in to comment.