The Bible uses two different approaches to the family line of Jesus. The list given in Matthew Chapter 1 is almost entirely different from the list in Luke Chapter 3. Here is a suggested explanation for that difference:
Matthew's list is obviously incomplete. For example, Matthew 1:11 says, "Josias (Josiah) begat Jechonias (Jehoiachin)." In reality Jehoiachin was Josiah's grandson. Matthew's purpose was not to list every single link in the genealogy, but rather simply to present enough of the line to demonstrate that Jesus was in fact descended from David and from Abraham (verse 1). In Matthew 1:17 he says, "So all the generations from Abraham to David are fourteen generations; and from David until the carrying away into Babylon are fourteen generations; and from the carrying away into Babylon unto Christ are fourteen generations." This doesn't have to mean that that is all the generations that there were, but simply that that is all the generations he was listing to establish Christ's legitimacy as King of Israel.
Whereas Matthew shows Jesus to have descended through the line of Judah's kings via Solomon, Luke's list presents Jesus as having descended through Nathan, another son of David and Bathsheba. I favor the explanation that suggests Luke is presenting the literal blood line of Jesus through Mary, and that Matthew is showing Jesus' legally recognized patriarchal line through Joseph. Both lines go through David. It appears that Mary's husband Joseph was the son of Jacob (Matthew 1:16), and that Heli (Luke 3:23) could have been Mary's father. If Mary was Heli's only child, Joseph would have become Heli's legal heir by marrying her. According to this explanation, Luke's account shows how Jesus was an actual blood descendant of David through Mary, and Matthew's account shows that Jesus was the legal heir of Judah's royal line though his adoption by Joseph.
This is consistent with the fact that Matthew tells the story of Jesus' birth from Joseph's perspective, while Luke tells Mary's story. And in Matthew's account, when the angel appears to Joseph in a dream he addresses him as "Joseph, thou son of David" (Matthew 1:20). The genealogy in the first seventeen verses was given to show how that is so.
Luke tells us that Mary was a relative of Elizabeth (Luke 1:36) who was descended from Aaron (verse 5). It is possible that Mary's father was of the tribe of Judah and that her mother was of the tribe of Levi. In that way, Jesus could be the blood descendant of David through His maternal grandfather, and Mary could still be related to Elisabeth on her mother's side.